public void BroadcastToOneType()
        {
            Messenger.Default = null;;
            var    recipient1 = new TestRecipient();
            var    recipient2 = new TestRecipient();
            var    recipient3 = new TestRecipient2();
            var    recipient4 = this;
            string message1   = null;
            string message2   = null;
            string message3   = null;
            string message4   = null;

            Messenger.Default.Register <string>(recipient1, x => message1 = x);
            Messenger.Default.Register <string>(recipient2, x => message2 = x);
            Messenger.Default.Register <string>(recipient3, x => message3 = x);
            Messenger.Default.Register <string>(recipient3, x => message3 = x);

            Messenger.Default.Send <string, TestRecipient>("test1");
            Assert.AreEqual("test1", message1, "message1");
            Assert.AreEqual("test1", message2, "message2");
            Assert.AreEqual("test1", message3, "message3");
            Assert.IsNull(message4);

            Messenger.Default.Send <string, TestRecipient2>("test2");
            Assert.AreEqual("test1", message1, "message1");
            Assert.AreEqual("test1", message2, "message2");
            Assert.AreEqual("test2", message3, "message3");
            Assert.IsNull(message4);
        }
        public void TestMultipleMessengerInstances()
        {
            var messenger1 = new Messenger();
            var messenger2 = new Messenger();

            var recipient11 = new TestRecipient1();
            var recipient12 = new TestRecipient1();
            var recipient21 = new TestRecipient2();
            var recipient22 = new TestRecipient2();

            messenger1.Register <string>(recipient11, recipient11.ReceiveMessage);
            messenger2.Register <string>(recipient12, recipient12.ReceiveMessage);
            messenger1.Register <string>(recipient21, recipient21.ReceiveMessage);
            messenger2.Register <string>(recipient22, recipient22.ReceiveMessage);

            const string TestContent1 = "abcd";
            const string TestContent2 = "efgh";

            Assert.AreEqual(null, recipient11.ReceivedContentString);
            Assert.AreEqual(null, recipient12.ReceivedContentString);
            Assert.AreEqual(null, recipient21.ReceivedContentString);
            Assert.AreEqual(null, recipient22.ReceivedContentString);

            messenger1.Send(TestContent1);

            Assert.AreEqual(TestContent1, recipient11.ReceivedContentString);
            Assert.AreEqual(null, recipient12.ReceivedContentString);
            Assert.AreEqual(TestContent1, recipient21.ReceivedContentString);
            Assert.AreEqual(null, recipient22.ReceivedContentString);

            messenger2.Send(TestContent2);

            Assert.AreEqual(TestContent1, recipient11.ReceivedContentString);
            Assert.AreEqual(TestContent2, recipient12.ReceivedContentString);
            Assert.AreEqual(TestContent1, recipient21.ReceivedContentString);
            Assert.AreEqual(TestContent2, recipient22.ReceivedContentString);
        }
        public void TestMultipleMessengerInstances()
        {
            var messenger1 = new Messenger();
            var messenger2 = new Messenger();

            var recipient11 = new TestRecipient1();
            var recipient12 = new TestRecipient1();
            var recipient21 = new TestRecipient2();
            var recipient22 = new TestRecipient2();

            messenger1.Register<string>(recipient11, recipient11.ReceiveMessage);
            messenger2.Register<string>(recipient12, recipient12.ReceiveMessage);
            messenger1.Register<string>(recipient21, recipient21.ReceiveMessage);
            messenger2.Register<string>(recipient22, recipient22.ReceiveMessage);

            const string TestContent1 = "abcd";
            const string TestContent2 = "efgh";

            Assert.AreEqual(null, recipient11.ReceivedContentString);
            Assert.AreEqual(null, recipient12.ReceivedContentString);
            Assert.AreEqual(null, recipient21.ReceivedContentString);
            Assert.AreEqual(null, recipient22.ReceivedContentString);

            messenger1.Send(TestContent1);

            Assert.AreEqual(TestContent1, recipient11.ReceivedContentString);
            Assert.AreEqual(null, recipient12.ReceivedContentString);
            Assert.AreEqual(TestContent1, recipient21.ReceivedContentString);
            Assert.AreEqual(null, recipient22.ReceivedContentString);

            messenger2.Send(TestContent2);

            Assert.AreEqual(TestContent1, recipient11.ReceivedContentString);
            Assert.AreEqual(TestContent2, recipient12.ReceivedContentString);
            Assert.AreEqual(TestContent1, recipient21.ReceivedContentString);
            Assert.AreEqual(TestContent2, recipient22.ReceivedContentString);
        }
示例#4
0
        public void TestBroadcastToOneType()
        {
            Messenger.Reset();

            var recipient11 = new TestRecipient1();
            var recipient12 = new TestRecipient1();
            var recipient21 = new TestRecipient2();
            var recipient22 = new TestRecipient2();

            Messenger.Default.Register <string>(recipient11, recipient11.ReceiveMessage);
            Messenger.Default.Register <string>(recipient12, recipient12.ReceiveMessage);
            Messenger.Default.Register <string>(recipient21, recipient21.ReceiveMessage);
            Messenger.Default.Register <string>(recipient22, recipient22.ReceiveMessage);

            const string TestContent1 = "abcd";
            const string TestContent2 = "efgh";

            Assert.AreEqual(null, recipient11.ReceivedContentString);
            Assert.AreEqual(null, recipient12.ReceivedContentString);
            Assert.AreEqual(null, recipient21.ReceivedContentString);
            Assert.AreEqual(null, recipient22.ReceivedContentString);

            Messenger.Default.Send <string, TestRecipient1>(TestContent1);

            Assert.AreEqual(TestContent1, recipient11.ReceivedContentString);
            Assert.AreEqual(TestContent1, recipient12.ReceivedContentString);
            Assert.AreEqual(null, recipient21.ReceivedContentString);
            Assert.AreEqual(null, recipient22.ReceivedContentString);

            Messenger.Default.Send <string, TestRecipient2>(TestContent2);

            Assert.AreEqual(TestContent1, recipient11.ReceivedContentString);
            Assert.AreEqual(TestContent1, recipient12.ReceivedContentString);
            Assert.AreEqual(TestContent2, recipient21.ReceivedContentString);
            Assert.AreEqual(TestContent2, recipient22.ReceivedContentString);
        }
        public void TestBroadcastToOneType()
        {
            Messenger.Reset();

            var recipient11 = new TestRecipient1();
            var recipient12 = new TestRecipient1();
            var recipient21 = new TestRecipient2();
            var recipient22 = new TestRecipient2();

            Messenger.Default.Register<string>(recipient11, recipient11.ReceiveMessage);
            Messenger.Default.Register<string>(recipient12, recipient12.ReceiveMessage);
            Messenger.Default.Register<string>(recipient21, recipient21.ReceiveMessage);
            Messenger.Default.Register<string>(recipient22, recipient22.ReceiveMessage);

            const string testContent1 = "abcd";
            const string testContent2 = "efgh";

            Assert.AreEqual(null, recipient11.ReceivedContentString);
            Assert.AreEqual(null, recipient12.ReceivedContentString);
            Assert.AreEqual(null, recipient21.ReceivedContentString);
            Assert.AreEqual(null, recipient22.ReceivedContentString);

            Messenger.Default.Send<string, TestRecipient1>(testContent1);

            Assert.AreEqual(testContent1, recipient11.ReceivedContentString);
            Assert.AreEqual(testContent1, recipient12.ReceivedContentString);
            Assert.AreEqual(null, recipient21.ReceivedContentString);
            Assert.AreEqual(null, recipient22.ReceivedContentString);

            Messenger.Default.Send<string, TestRecipient2>(testContent2);

            Assert.AreEqual(testContent1, recipient11.ReceivedContentString);
            Assert.AreEqual(testContent1, recipient12.ReceivedContentString);
            Assert.AreEqual(testContent2, recipient21.ReceivedContentString);
            Assert.AreEqual(testContent2, recipient22.ReceivedContentString);
        }
        public void TestBroadcastToOneInterface()
        {
            Messenger.Reset();

            var recipient11 = new TestRecipient1();
            var recipient12 = new TestRecipient1();
            var recipient21 = new TestRecipient2();
            var recipient22 = new TestRecipient2();
            var recipient31 = new TestRecipient3();
            var recipient32 = new TestRecipient3();

            Messenger.Default.Register<string>(recipient11, recipient11.ReceiveMessage);
            Messenger.Default.Register<string>(recipient12, recipient12.ReceiveMessage);
            Messenger.Default.Register<string>(recipient21, recipient21.DoSomething);
            Messenger.Default.Register<string>(recipient22, recipient22.DoSomething);
            Messenger.Default.Register<string>(recipient31, recipient31.DoSomething);
            Messenger.Default.Register<string>(recipient32, recipient32.DoSomething);

            const string testContent1 = "abcd";

            Assert.AreEqual(null, recipient11.ReceivedContentString);
            Assert.AreEqual(null, recipient12.ReceivedContentString);
            Assert.AreEqual(null, recipient21.ReceivedContentString);
            Assert.AreEqual(null, recipient22.ReceivedContentString);
            Assert.AreEqual(null, recipient31.ReceivedContentString);
            Assert.AreEqual(null, recipient32.ReceivedContentString);

            Messenger.Default.Send<string, ITestRecipient>(testContent1);

            Assert.AreEqual(null, recipient11.ReceivedContentString);
            Assert.AreEqual(null, recipient12.ReceivedContentString);
            Assert.AreEqual(testContent1, recipient21.ReceivedContentString);
            Assert.AreEqual(testContent1, recipient22.ReceivedContentString);
            Assert.AreEqual(testContent1, recipient31.ReceivedContentString);
            Assert.AreEqual(testContent1, recipient32.ReceivedContentString);
        }
        public void TestBroadcastToOneInterface()
        {
            Messenger.Reset();

            var recipient11 = new TestRecipient1();
            var recipient12 = new TestRecipient1();
            var recipient21 = new TestRecipient2();
            var recipient22 = new TestRecipient2();
            var recipient31 = new TestRecipient3();
            var recipient32 = new TestRecipient3();

            Messenger.Default.Register <string>(recipient11, recipient11.ReceiveMessage);
            Messenger.Default.Register <string>(recipient12, recipient12.ReceiveMessage);
            Messenger.Default.Register <string>(recipient21, recipient21.DoSomething);
            Messenger.Default.Register <string>(recipient22, recipient22.DoSomething);
            Messenger.Default.Register <string>(recipient31, recipient31.DoSomething);
            Messenger.Default.Register <string>(recipient32, recipient32.DoSomething);

            const string TestContent1 = "abcd";

            Assert.AreEqual(null, recipient11.ReceivedContentString);
            Assert.AreEqual(null, recipient12.ReceivedContentString);
            Assert.AreEqual(null, recipient21.ReceivedContentString);
            Assert.AreEqual(null, recipient22.ReceivedContentString);
            Assert.AreEqual(null, recipient31.ReceivedContentString);
            Assert.AreEqual(null, recipient32.ReceivedContentString);

            Messenger.Default.Send <string, ITestRecipient>(TestContent1);

            Assert.AreEqual(null, recipient11.ReceivedContentString);
            Assert.AreEqual(null, recipient12.ReceivedContentString);
            Assert.AreEqual(TestContent1, recipient21.ReceivedContentString);
            Assert.AreEqual(TestContent1, recipient22.ReceivedContentString);
            Assert.AreEqual(TestContent1, recipient31.ReceivedContentString);
            Assert.AreEqual(TestContent1, recipient32.ReceivedContentString);
        }