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 TypeTest() { var target = new MessageMediator(); var tc = new TestCounter(); target.Register(tc); bool hadTarget = target.SendMessage("Test2"); Assert.AreEqual(true, hadTarget, "Mediator did not return success"); hadTarget = target.SendMessage("test3", "Test2"); Assert.AreEqual(true, hadTarget, "Mediator did not return success"); Assert.AreEqual <int>(2, tc.Count); }
/// <summary> /// Method to invoke when the AddEmployee command is executed. /// </summary> private void OnAddEmployeeExecute() { var employee = new Employee() { Department = SelectedDepartment }; var typeFactory = TypeFactory.Default; var viewModel = typeFactory.CreateInstanceWithParametersAndAutoCompletion <EmployeeViewModel>(employee); if (!(_uiVisualizerService.ShowDialog(viewModel) ?? false)) { return; } _employeeRepository.AddEmployee(employee); if (employee.Department == SelectedDepartment) { Employees.Add(employee); } MessageMediator.SendMessage(employee.Department, "UpdateSelectedDepartmentFromEM"); Mediator.SendMessage(string.Format("Employee {0} {1} is added in department {2}", employee.FirstName, employee.LastName, employee.Department.Name), "UpdateNotification"); }
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 NoRegisterTest() { var target = new MessageMediator(); bool passedTest = target.SendMessage("test", "Test"); Assert.AreEqual(false, passedTest, "Mediator returned success"); }
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 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 NoRegisterTest() { var target = new MessageMediator(); bool passedTest = target.SendMessage("test", "Test"); Assert.IsFalse(passedTest, "Mediator located unregistered target?"); }
public void RegisterSingleHandler() { var target = new MessageMediator(); bool passedTest = false; Action <string> handler = o => passedTest = (o == "Test"); target.RegisterHandler("test", handler); bool hadTarget = target.SendMessage("test", "Test"); Assert.IsTrue(hadTarget, "Mediator did not return success"); Assert.IsTrue(passedTest, "Did not receive message"); target.UnregisterHandler("test", handler); Assert.IsFalse(target.SendMessage("test", "Test")); }
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 TestIntParam2() { var target = new MessageMediator(); var tc = new TestCounter(); target.Register(tc); target.RegisterHandler <int>("test6", tc.MessageHandler6); bool hadTarget = target.SendMessage(5); Assert.AreEqual(false, hadTarget, "Mediator found unexpected target"); hadTarget = target.SendMessage("test6", 5); Assert.AreEqual(true, hadTarget, "Mediator did not find target"); Assert.AreEqual <int>(5, tc.Count); }
public void TestJustInterfaces() { var target = new MessageMediator(); var tc = new TestImplJustInterfaces(); target.Register(tc); bool hadTarget = target.SendMessage(new DataEvent("Test")); Assert.AreEqual(true, hadTarget, "Mediator did not find target"); hadTarget = target.SendMessage(new DataEventUpper("Test")); Assert.AreEqual(true, hadTarget, "Mediator did not find target"); hadTarget = target.SendMessage(new DataEventLower("Test")); Assert.AreEqual(true, hadTarget, "Mediator did not find target"); }
private void CreateMessage_Click(object sender, EventArgs e) { if (ValidateChildren(ValidationConstraints.Enabled)) { MessageMediator.MessageModel = NameMessage; MessageMediator.SendMessage(); this.Close(); } }
/// <summary> /// Method to invoke when the Select command is executed. /// </summary> private void OnSelectExecute() { if (ObjectHelper.IsNull(SelectedDepartment)) { return; } MessageMediator.SendMessage(SelectedDepartment.Name, "UpdateEmployees"); MessageMediator.SendMessage(SelectedDepartment, "UpdateSelectedDepartmentFromDM"); }
public void TestIntParam() { var target = new MessageMediator(); var tc = new TestCounter(); target.Register(tc); bool hadTarget = target.SendMessage(5); Assert.IsFalse(hadTarget, "Mediator found unexpected target"); target.RegisterHandler <int>(tc.MessageHandler5); hadTarget = target.SendMessage(5); Assert.IsTrue(hadTarget, "Mediator did not find target"); Assert.AreEqual(5, tc.Count); target.Unregister(tc); target.UnregisterHandler <int>(tc.MessageHandler5); Assert.IsFalse(target.SendMessage(5)); }
public void SingleRegisterTest() { var target = new MessageMediator(); bool passedTest = false; target.RegisterHandler("test", (string o) => passedTest = (o == "Test")); bool hadTarget = target.SendMessage("test", "Test"); Assert.AreEqual(true, hadTarget, "Mediator did not return success"); Assert.AreEqual(true, passedTest, "Did not receive message"); }
public void DeadInstanceTest() { var target = new MessageMediator(); target.Register(new TestCounter()); GC.Collect(); bool hadTarget = target.SendMessage("test2", "Test2"); Assert.AreEqual(true, hadTarget, "Mediator did not return success"); }
public void TestInterface2() { var target = new MessageMediator(); var tc = new Test3(); target.Register(tc); bool hadTarget = target.SendMessage <IData>(new Data("Test")); Assert.AreEqual(true, hadTarget, "Mediator did not find target"); Assert.AreEqual <int>(1, tc.Count); }
public void TestInterface() { var target = new MessageMediator(); var tc = new TestImpl(); target.Register(tc); bool hadTarget = target.SendMessage <IDataEvent>(new DataEvent("Test")); Assert.AreEqual(true, hadTarget, "Mediator did not find target"); // Should invoke two methods - interface AND direct Assert.AreEqual(5, tc.Count); }
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 ShowErrorTest() { var ErrorWindowService = new ErrorWindowService(); var QuestionWindowService = new QuestionWindowService(); var EnterPinWindowService = new EnterPinWindowService(); AuthorizationService = new AuthorizationService(); StationRepository = MockRepository.GenerateStub <IStationRepository>(); LanguageRepository = MockRepository.GenerateStub <ILanguageRepository>(); LineProvider = MockRepository.GenerateStub <ILineProvider>(); BusinessPropsHelper = MockRepository.GenerateStub <IBusinessPropsHelper>(); DataBinding = MockRepository.GenerateStub <IDataBinding>(); IoCContainer.Kernel.Bind <IDataBinding>().ToConstant <IDataBinding>(DataBinding).InSingletonScope(); IoCContainer.Kernel.Bind <IAuthorizationService>().ToConstant <IAuthorizationService>(AuthorizationService).InSingletonScope(); IoCContainer.Kernel.Bind <IStationRepository>().ToConstant <IStationRepository>(StationRepository).InSingletonScope(); IoCContainer.Kernel.Bind <ILanguageRepository>().ToConstant <ILanguageRepository>(LanguageRepository).InSingletonScope(); IoCContainer.Kernel.Bind <ILineProvider>().ToConstant <ILineProvider>(LineProvider).InSingletonScope(); IoCContainer.Kernel.Bind <IBusinessPropsHelper>().ToConstant <IBusinessPropsHelper>(BusinessPropsHelper).InSingletonScope(); IoCContainer.Kernel.Bind <IErrorWindowService>().ToConstant <IErrorWindowService>(ErrorWindowService).InSingletonScope(); IoCContainer.Kernel.Bind <IQuestionWindowService>().ToConstant <IQuestionWindowService>(QuestionWindowService).InSingletonScope(); IoCContainer.Kernel.Bind <IEnterPinWindowService>().ToConstant <IEnterPinWindowService>(EnterPinWindowService).InSingletonScope(); DataBinding.Expect(x => x.TipListInfo).Return(new TipListInfo()); SessionWS sessionId = new SessionWS(); sessionId.balance = new accountBalance(); sessionId.username = "******"; SessionWS sessionIdNull = null; WsdlRepository.BackToRecord(); WsdlRepository.Expect(x => x.OpenSession("", true, "", "", false)).Return(new SessionWS()).IgnoreArguments(); WsdlRepository.Expect(x => x.ValidateIdCard("1234", "0024", false, out sessionId)).Return(true).OutRef(sessionIdNull); WsdlRepository.Expect(x => x.LoginWithIdCard("0024", "1234", "1234")).Return(sessionId); WsdlRepository.Replay(); StationRepository.TurnOffCashInInit = true; StationRepository.Expect(x => x.AllowAnonymousBetting).Return(true); StationRepository.Expect(x => x.IsReady).Return(true); StationRepository.Expect(x => x.Active).Return(1); StationRepository.Expect(x => x.StationNumber).Return("0024"); StationRepository.Expect(x => x.HubSettings).Return(new Dictionary <string, string>()); StationRepository.Currency = "EUR"; ChangeTracker = IoCContainer.Kernel.Get <IChangeTracker>(); ChangeTracker.CurrentUser = new AnonymousUser("1", 1); ChangeTracker.CurrentUser.Cashpool = 100000; ChangeTracker.CurrentUser.AvailableCash = 100000; BaseClass.Dispatcher.Invoke(() => { BaseClass.Window = MyRegionManager.FindWindowByViewModel <MainViewModel>(); BaseClass.Window.Show(); }); Thread.Sleep(1000); var mainModel = MyRegionManager.CurrentViewModelInRegion(RegionNames.ContentRegion); Task.Run(() => { MessageMediator.SendMessage("1234", MsgTag.CardInserted); }); while (EnterPinWindowService.Model == null) { Thread.Sleep(10); } Thread.Sleep(1000); EnterPinWindowService.Model.Pin = "1234"; EnterPinWindowService.Model.PinMasked = "1234"; EnterPinWindowService.Model.ProceedCommand.Execute(""); Task.Run(() => { mainModel.ShowMessage("msg1"); }); Thread.Sleep(1000); QuestionWindowService.Model.YesCommand.Execute(""); Task.Run(() => { mainModel.ShowError("error1"); }); Thread.Sleep(1000); ErrorWindowService.Model.OKCommand.Execute(""); Task.Run(() => { mainModel.ShowMessage("msg2"); }); Thread.Sleep(1000); QuestionWindowService.Model.YesCommand.Execute(""); Task.Run(() => { mainModel.ShowError("error2"); }); Thread.Sleep(1000); ErrorWindowService.Model.OKCommand.Execute(""); Task.Run(() => { QuestionWindowService.ShowMessage("mgs3", null, null, null, null, false, 1); }); Thread.Sleep(3000); BaseClass.Dispatcher.Invoke(() => { BaseClass.Window.Close(); }); }
public void Show2ErrorsTest() { var ErrorWindowService = new ErrorWindowService(); var QuestionWindowService = new QuestionWindowService(); var EnterPinWindowService = new EnterPinWindowService(); AuthorizationService = new AuthorizationService(); StationRepository = MockRepository.GenerateStub <IStationRepository>(); LanguageRepository = MockRepository.GenerateStub <ILanguageRepository>(); LineProvider = MockRepository.GenerateStub <ILineProvider>(); BusinessPropsHelper = MockRepository.GenerateStub <IBusinessPropsHelper>(); DataBinding = MockRepository.GenerateStub <IDataBinding>(); IoCContainer.Kernel.Bind <IDataBinding>().ToConstant <IDataBinding>(DataBinding).InSingletonScope(); IoCContainer.Kernel.Bind <IAuthorizationService>().ToConstant <IAuthorizationService>(AuthorizationService).InSingletonScope(); IoCContainer.Kernel.Bind <IStationRepository>().ToConstant <IStationRepository>(StationRepository).InSingletonScope(); IoCContainer.Kernel.Bind <ILanguageRepository>().ToConstant <ILanguageRepository>(LanguageRepository).InSingletonScope(); IoCContainer.Kernel.Bind <ILineProvider>().ToConstant <ILineProvider>(LineProvider).InSingletonScope(); IoCContainer.Kernel.Bind <IBusinessPropsHelper>().ToConstant <IBusinessPropsHelper>(BusinessPropsHelper).InSingletonScope(); IoCContainer.Kernel.Bind <IErrorWindowService>().ToConstant <IErrorWindowService>(ErrorWindowService).InSingletonScope(); IoCContainer.Kernel.Bind <IQuestionWindowService>().ToConstant <IQuestionWindowService>(QuestionWindowService).InSingletonScope(); IoCContainer.Kernel.Bind <IEnterPinWindowService>().ToConstant <IEnterPinWindowService>(EnterPinWindowService).InSingletonScope(); DataBinding.Expect(x => x.TipListInfo).Return(new TipListInfo()); StationRepository.TurnOffCashInInit = true; StationRepository.Expect(x => x.AllowAnonymousBetting).Return(true); StationRepository.Expect(x => x.IsReady).Return(true); StationRepository.Expect(x => x.Active).Return(1); StationRepository.Expect(x => x.StationNumber).Return("0024"); StationRepository.Expect(x => x.HubSettings).Return(new Dictionary <string, string>()); StationRepository.Currency = "EUR"; ChangeTracker = IoCContainer.Kernel.Get <IChangeTracker>(); ChangeTracker.CurrentUser = new AnonymousUser("1", 1); ChangeTracker.CurrentUser.Cashpool = 100000; ChangeTracker.CurrentUser.AvailableCash = 100000; Dispatcher.Invoke(() => { Window = MyRegionManager.FindWindowByViewModel <MainViewModel>(); Window.Show(); }); var mainModel = MyRegionManager.CurrentViewModelInRegion(RegionNames.ContentRegion); while (!mainModel.IsReady) { Thread.Sleep(1); } new Thread(() => { Task.Delay(1); MessageMediator.SendMessage <Tuple <string, string, bool, int> >(new Tuple <string, string, bool, int>("LostInternetConnection", "", false, 0), MsgTag.Error); MessageMediator.SendMessage <Tuple <string, string, bool, int> >(new Tuple <string, string, bool, int>("LostInternetConnection", "", false, 0), MsgTag.Error); }).Start(); MessageMediator.SendMessage <Tuple <string, string, bool, int> >(new Tuple <string, string, bool, int>("LostInternetConnection", "", false, 0), MsgTag.Error); int windowCount = 0; Thread.Sleep(1000); Dispatcher.Invoke(() => { windowCount = App.Current.Windows.Count; }); Assert.AreEqual(4, windowCount); Thread.Sleep(1000); Dispatcher.Invoke(() => { windowCount = App.Current.Windows.Count; }); Assert.AreEqual(1, windowCount); Dispatcher.Invoke(() => { Window.Close(); }); }