Beispiel #1
0
        public void TestUnregisterForOneType()
        {
            Messenger.Reset();

            const string TestString1 = "Hello world";
            const string TestString2 = "Another message";
            var now = DateTime.Now;
            var later = DateTime.Now + TimeSpan.FromDays(1);

            var recipient = new OldTestRecipient();
            Messenger.Default.Register(recipient, typeof(GenericMessage<string>), false);
            Messenger.Default.Register(recipient, typeof(CommandMessage<DateTime>), false);

            var recipient2 = new OldTestRecipient();
            Messenger.Default.Register(recipient2, typeof(GenericMessage<string>), false);
            Messenger.Default.Register(recipient2, typeof(CommandMessage<DateTime>), false);

            var message1 = new GenericMessage<string>(this, TestString1);
            Messenger.Default.Broadcast(message1);

            var message2 = new CommandMessage<DateTime>(this, now, OldTestCommandHost.SaveCommand);
            Messenger.Default.Broadcast(message2);

            Assert.AreEqual(TestString1, recipient.Content);
            Assert.AreEqual(TestString1, recipient2.Content);
            Assert.AreEqual(now, recipient.DateTimeContent);
            Assert.AreEqual(now, recipient2.DateTimeContent);

            Messenger.Default.Unregister(recipient, typeof(GenericMessage<string>));

            var message3 = new GenericMessage<string>(this, TestString2);
            Messenger.Default.Broadcast(message3);

            var message4 = new CommandMessage<DateTime>(this, later, OldTestCommandHost.SaveCommand);
            Messenger.Default.Broadcast(message4);

            Assert.AreEqual(TestString1, recipient.Content);
            Assert.AreEqual(TestString2, recipient2.Content);
            Assert.AreEqual(later, recipient.DateTimeContent);
            Assert.AreEqual(later, recipient2.DateTimeContent);
        }
Beispiel #2
0
        public void ExecuteTestGeneric(object sender, object target)
        {
            var testContent1 = new InvalidOperationException();
            var testContent2 = new InvalidCastException();

            Exception receivedContent1 = null;
            Exception receivedContent2 = null;

            object receivedSender = null;
            object receivedTarget = null;

            Messenger.Reset();

            Messenger.Default.Register<CommandMessage<Exception>>(this,
                                                                  m =>
                                                                  {
                                                                      receivedSender = m.Sender;
                                                                      receivedTarget = m.Target;

                                                                      if (m.Command == DummyCommand1)
                                                                      {
                                                                          receivedContent1 = testContent1;
                                                                          return;
                                                                      }

                                                                      if (m.Command == DummyCommand2)
                                                                      {
                                                                          receivedContent2 = testContent2;
                                                                          return;
                                                                      }
                                                                  });

            Assert.AreEqual(null, receivedContent1);
            Assert.AreEqual(null, receivedContent2);

            CommandMessage<Exception> commandMessage1;
            CommandMessage<Exception> commandMessage2;

            if (sender == null)
            {
                commandMessage1 = new CommandMessage<Exception>(testContent1, DummyCommand1);
                commandMessage2 = new CommandMessage<Exception>(testContent2, DummyCommand2);
            }
            else
            {
                if (target == null)
                {
                    commandMessage1 = new CommandMessage<Exception>(sender, testContent1, DummyCommand1);
                    commandMessage2 = new CommandMessage<Exception>(sender, testContent2, DummyCommand2);
                }
                else
                {
                    commandMessage1 = new CommandMessage<Exception>(sender, target, testContent1, DummyCommand1);
                    commandMessage2 = new CommandMessage<Exception>(sender, target, testContent2, DummyCommand2);
                }
            }

            Messenger.Default.Send(commandMessage1);

            Assert.AreEqual(sender, receivedSender);
            Assert.AreEqual(target, receivedTarget);
            Assert.AreEqual(testContent1, receivedContent1);
            Assert.AreEqual(null, receivedContent2);

            receivedSender = null;
            receivedTarget = null;

            Messenger.Default.Send(commandMessage2);

            Assert.AreEqual(sender, receivedSender);
            Assert.AreEqual(target, receivedTarget);
            Assert.AreEqual(testContent1, receivedContent1);
            Assert.AreEqual(testContent2, receivedContent2);
        }
Beispiel #3
0
 private void ReceiveCommandMessage(CommandMessage<DateTime> message)
 {
     if (message.Command
         == OldTestCommandHost.SaveCommand)
     {
         DateTimeContent = message.Content;
     }
 }
Beispiel #4
0
        private void ExecuteTest(object sender, object target)
        {
            const string TestContent1 = "abcd";
            const string TestContent2 = "efgh";

            string receivedContent1 = null;
            string receivedContent2 = null;

            object receivedSender = null;
            object receivedTarget = null;

            Messenger.Reset();

            Messenger.Default.Register<CommandMessage>(this,
                                                       m =>
                                                       {
                                                           receivedSender = m.Sender;
                                                           receivedTarget = m.Target;

                                                           if (m.Command == DummyCommand1)
                                                           {
                                                               receivedContent1 = TestContent1;
                                                               return;
                                                           }

                                                           if (m.Command == DummyCommand2)
                                                           {
                                                               receivedContent2 = TestContent2;
                                                               return;
                                                           }
                                                       });

            Assert.AreEqual(null, receivedContent1);
            Assert.AreEqual(null, receivedContent2);

            CommandMessage commandMessage1;
            CommandMessage commandMessage2;

            if (sender == null)
            {
                commandMessage1 = new CommandMessage(DummyCommand1);
                commandMessage2 = new CommandMessage(DummyCommand2);
            }
            else
            {
                if (target == null)
                {
                    commandMessage1 = new CommandMessage(sender, DummyCommand1);
                    commandMessage2 = new CommandMessage(sender, DummyCommand2);
                }
                else
                {
                    commandMessage1 = new CommandMessage(sender, target, DummyCommand1);
                    commandMessage2 = new CommandMessage(sender, target, DummyCommand2);
                }
            }

            Messenger.Default.Send(commandMessage1);

            Assert.AreEqual(sender, receivedSender);
            Assert.AreEqual(target, receivedTarget);
            Assert.AreEqual(TestContent1, receivedContent1);
            Assert.AreEqual(null, receivedContent2);

            receivedTarget = null;
            receivedSender = null;

            Messenger.Default.Send(commandMessage2);

            Assert.AreEqual(sender, receivedSender);
            Assert.AreEqual(target, receivedTarget);
            Assert.AreEqual(TestContent1, receivedContent1);
            Assert.AreEqual(TestContent2, receivedContent2);
        }