public void AddOrderFromOrderVMTest() { AddEditOrderViewModel addEditOrderViewModel = new AddEditOrderViewModel(); addEditOrderViewModel.CurrentViewMode = ViewMode.AddMode; addEditOrderViewModel.CurrentCustomer = CustomerModel.CustomerToCustomerModel(cust); addEditOrderViewModel.CurrentCustomerOrder = OrderModel.OrderToOrderModel(ord); addEditOrderViewModel.CurrentCustomerOrder.Quantity.DataValue = 1; AddEditCustomerViewModel addEditCustomerViewModel = new AddEditCustomerViewModel(); addEditCustomerViewModel.CurrentCustomer = CustomerModel.CustomerToCustomerModel(cust); //As setting the AddEditCustomerViewModel.CurrentCustomer causes //a background fetch of all CurrentCustomer.Orders, we need to wait //until that completes to continue ManualResetEvent manualEvent = new ManualResetEvent(false); addEditCustomerViewModel.BgWorker.BackgroundTaskCompleted += delegate(object sender, EventArgs args) { // Signal the waiting NUnit thread that we're ready to move on. manualEvent.Set(); }; //Wait for signal to move on from BackgroundTaskManager.BackgroundTaskCompleted manualEvent.WaitOne(5000, false); //Execute AddEditOrderViewModel.SaveOrderCommand addEditOrderViewModel.SaveOrderCommand.Execute(null); Int32 currentCustomerOrderCount = addEditCustomerViewModel.CurrentCustomer.Orders.Count; //Wait for Lazy load of AddEditCustomerViewModel.CurrentCustomer.Orders //which is triggered via Mediator, and is run on the AddEditCustomerViewModel //BackgroundTaskManager manualEvent = new ManualResetEvent(false); addEditCustomerViewModel.BgWorker.BackgroundTaskCompleted += delegate(object sender, EventArgs args) { // Signal the waiting NUnit thread that we're ready to move on. manualEvent.Set(); }; //Wait for signal to move on from BackgroundTaskManager.BackgroundTaskCompleted manualEvent.WaitOne(5000, false); Assert.AreEqual(currentCustomerOrderCount + 1, addEditCustomerViewModel.CurrentCustomer.Orders.Count); }
private void DeleteCustomerMessageSink(CustomerModel customerToDelete) { bool okToDeleteCustomer = true; foreach (var item in workspaces) { if (item.GetType().Equals(typeof(AddEditCustomerViewModel))) { AddEditCustomerViewModel vm = item as AddEditCustomerViewModel; if (vm.CurrentCustomer.CustomerId.DataValue == customerToDelete.CustomerId.DataValue) { okToDeleteCustomer = false; break; } } } Mediator.NotifyColleagues <Boolean>("OkToDeleteCustomerMessage", okToDeleteCustomer); }
private void EditCustomerMessageSink(CustomerModel editableCustomer) { if (!IsNewWorkSpaceItemAllowed <AddEditCustomerViewModel>()) { messageBoxService.ShowWarning( "There is already an Add/Edit View open\r\n\r\n" + "This application only allows 1 active Add/Edit view\r\n" + "to be opened at 1 time"); return; } AddEditCustomerViewModel addEditCustomerViewModel = new AddEditCustomerViewModel(); addEditCustomerViewModel.IsCloseable = true; addEditCustomerViewModel.CurrentCustomer = editableCustomer; addEditCustomerViewModel.CurrentViewMode = ViewMode.EditMode; Workspaces.Add(addEditCustomerViewModel); this.SetActiveWorkspace(addEditCustomerViewModel); }
/// <summary> /// Executes the AddCustomerCommand /// </summary> private void ExecuteAddCustomerCommand() { AddCustomerCommand.CommandSucceeded = false; if (!IsNewWorkSpaceItemAllowed <AddEditCustomerViewModel>()) { messageBoxService.ShowWarning( "There is already an Add/Edit View open\r\n\r\n" + "This application only allows 1 active Add/Edit view\r\n" + "to be opened at 1 time"); return; } AddEditCustomerViewModel addEditCustomerViewModel = new AddEditCustomerViewModel(); addEditCustomerViewModel.IsCloseable = true; addEditCustomerViewModel.CurrentViewMode = ViewMode.AddMode; Workspaces.Add(addEditCustomerViewModel); this.SetActiveWorkspace(addEditCustomerViewModel); AddCustomerCommand.CommandSucceeded = true; }
private void EditCustomerCommandTest(AddEditCustomerViewModel addEditCustomerViewModel) { //Test that EditCustomerCommand can not execute with no current order addEditCustomerViewModel.CurrentCustomer = null; //As setting the AddEditCustomerViewModel.CurrentCustomer causes //a background fetch of all CurrentCustomer.Orders, we need to wait //until that completes to continue ManualResetEvent manualEvent = new ManualResetEvent(false); addEditCustomerViewModel.BgWorker.BackgroundTaskCompleted += delegate(object sender, EventArgs args) { // Signal the waiting NUnit thread that we're ready to move on. manualEvent.Set(); }; //Wait for signal to move on from BackgroundTaskManager.BackgroundTaskCompleted manualEvent.WaitOne(5000, false); Assert.AreEqual(addEditCustomerViewModel.EditCustomerCommand.CanExecute(null), false); //now give it an Customer, and check that EditCustomerCommand can't run until its in correct mode addEditCustomerViewModel.CurrentCustomer = CustomerModel.CustomerToCustomerModel(cust); addEditCustomerViewModel.CurrentViewMode = ViewMode.EditMode; Assert.AreEqual(addEditCustomerViewModel.EditCustomerCommand.CanExecute(null), false); //now allow the EditCustomerCommand to run by placing it in the correct mode addEditCustomerViewModel.CurrentViewMode = ViewMode.ViewOnlyMode; Assert.AreEqual(addEditCustomerViewModel.EditCustomerCommand.CanExecute(null), true); //execute the EditCustomerCommand addEditCustomerViewModel.EditCustomerCommand.Execute(null); Assert.AreEqual(addEditCustomerViewModel.CurrentViewMode, ViewMode.EditMode); Assert.AreEqual(addEditCustomerViewModel.EditCustomerCommand.CommandSucceeded, true); }
public void CancelCustomerCommandTest() { AddEditCustomerViewModel addEditCustomerViewModel = new AddEditCustomerViewModel(); //test the edit command first, which allows us to put //order into edit mode EditCustomerCommandTest(addEditCustomerViewModel); //so now make an edit to the Customer, say change MobilePhoneNumber Int32 editPhoneNum = 9999; cust.MobilePhoneNumber = editPhoneNum.ToString(); #region CancelCustomerCommandTests //check that CancelCustomerCommand can't run until its in correct mode addEditCustomerViewModel.CurrentViewMode = ViewMode.ViewOnlyMode; Assert.AreEqual(addEditCustomerViewModel.CancelCustomerCommand.CanExecute(null), false); //now allow the CancelCustomerCommand to run by placing it in the correct mode addEditCustomerViewModel.CurrentViewMode = ViewMode.EditMode; Assert.AreEqual(addEditCustomerViewModel.CancelCustomerCommand.CanExecute(null), true); //execute the CancelCustomerCommand addEditCustomerViewModel.CancelCustomerCommand.Execute(null); Assert.AreNotEqual(addEditCustomerViewModel.CurrentCustomer.HomePhoneNumber.DataValue, editPhoneNum); Assert.AreEqual(addEditCustomerViewModel.CancelCustomerCommand.CommandSucceeded, true); //Test that CancelCustomerCommand can not execute with out a current Customer addEditCustomerViewModel.CurrentCustomer = null; Assert.AreEqual(addEditCustomerViewModel.CancelCustomerCommand.CanExecute(null), false); #endregion }
public void SaveCustomerCommandTest() { AddEditCustomerViewModel addEditCustomerViewModel = new AddEditCustomerViewModel(); //Test Command can't run without an order Assert.AreEqual(addEditCustomerViewModel.SaveCustomerCommand.CanExecute(null), false); #region AddMode addEditCustomerViewModel.CurrentViewMode = ViewMode.AddMode; Customer newCust = GetCustomer("blah", "more"); addEditCustomerViewModel.CurrentCustomer = CustomerModel.CustomerToCustomerModel(newCust); //As setting the AddEditCustomerViewModel.CurrentCustomer causes //a background fetch of all CurrentCustomer.Orders, we need to wait //until that completes to continue ManualResetEvent manualEvent = new ManualResetEvent(false); addEditCustomerViewModel.BgWorker.BackgroundTaskCompleted += delegate(object sender, EventArgs args) { // Signal the waiting NUnit thread that we're ready to move on. manualEvent.Set(); }; //Wait for signal to move on from BackgroundTaskManager.BackgroundTaskCompleted manualEvent.WaitOne(5000, false); //test Save Command can run Assert.AreEqual(addEditCustomerViewModel.SaveCustomerCommand.CanExecute(null), true); //Execute SaveCommand addEditCustomerViewModel.SaveCustomerCommand.Execute(null); Assert.Greater(addEditCustomerViewModel.CurrentCustomer.CustomerId.DataValue, 0); #endregion #region EditMode addEditCustomerViewModel.CurrentViewMode = ViewMode.EditMode; addEditCustomerViewModel.CurrentCustomer = CustomerModel.CustomerToCustomerModel(cust); //As setting the AddEditCustomerViewModel.CurrentCustomer causes //a background fetch of all CurrentCustomer.Orders, we need to wait //until that completes to continue manualEvent = new ManualResetEvent(false); addEditCustomerViewModel.BgWorker.BackgroundTaskCompleted += delegate(object sender, EventArgs args) { // Signal the waiting NUnit thread that we're ready to move on. manualEvent.Set(); }; //Wait for signal to move on from BackgroundTaskManager.BackgroundTaskCompleted manualEvent.WaitOne(5000, false); //test Save Command can run Assert.AreEqual(addEditCustomerViewModel.SaveCustomerCommand.CanExecute(null), true); //Execute SaveCustomerCommand addEditCustomerViewModel.SaveCustomerCommand.Execute(null); Assert.AreEqual(addEditCustomerViewModel.SaveCustomerCommand.CommandSucceeded, true); #endregion }
public void DeleteOrderTest() { AddEditCustomerViewModel addEditCustomerViewModel = new AddEditCustomerViewModel(); //Test Command can't run without an order Assert.AreEqual(addEditCustomerViewModel.DeleteOrderCommand.CanExecute(null), false); addEditCustomerViewModel.CurrentCustomer = CustomerModel.CustomerToCustomerModel(cust); //As setting the AddEditCustomerViewModel.CurrentCustomer causes //a background fetch of all CurrentCustomer.Orders, we need to wait //until that completes to continue ManualResetEvent manualEvent = new ManualResetEvent(false); addEditCustomerViewModel.BgWorker.BackgroundTaskCompleted += delegate(object sender, EventArgs args) { // Signal the waiting NUnit thread that we're ready to move on. manualEvent.Set(); }; //Wait for signal to move on from BackgroundTaskManager.BackgroundTaskCompleted manualEvent.WaitOne(5000, false); addEditCustomerViewModel.CurrentCustomerOrder = addEditCustomerViewModel.CurrentCustomer.Orders.First(); addEditCustomerViewModel.CurrentCustomerOrder.Quantity.DataValue = 1; //Test that DeleteOrderCommand can now Execute Assert.AreEqual(addEditCustomerViewModel.DeleteOrderCommand.CanExecute(null), true); #region Test NO, delete MessageBox option //Run the DeleteOrderCommand, with a NO, Do not delete MessageBox option Queued up TestMessageBoxService testMessageBoxService = (TestMessageBoxService) ViewModelBase.ServiceProvider.Resolve<IMessageBoxService>(); //Queue up the response we expect for our given TestMessageBoxService //for a given ICommand/Method call within the test ViewModel testMessageBoxService.ShowYesNoResponders.Enqueue (() => { return CustomDialogResults.No; } ); Int32 existingCustomerOrdersCount = addEditCustomerViewModel.CurrentCustomer.Orders.Count(); addEditCustomerViewModel.DeleteOrderCommand.Execute(null); //Clear the TestMessageBoxService.ShowYesNoResponders testMessageBoxService.ShowYesNoResponders.Clear(); Assert.AreEqual(existingCustomerOrdersCount, addEditCustomerViewModel.CurrentCustomer.Orders.Count()); #endregion #region Test YES, delete MessageBox option //Run the DeleteOrderCommand, with a YES, Do delete MessageBox option Queued up //Queue up the response we expect for our given TestMessageBoxService //for a given ICommand/Method call within the test ViewModel testMessageBoxService.ShowYesNoResponders.Enqueue (() => { return CustomDialogResults.Yes; } ); existingCustomerOrdersCount = addEditCustomerViewModel.CurrentCustomer.Orders.Count(); addEditCustomerViewModel.DeleteOrderCommand.Execute(null); //Clear the TestMessageBoxService.ShowYesNoResponders testMessageBoxService.ShowYesNoResponders.Clear(); manualEvent = new ManualResetEvent(false); addEditCustomerViewModel.BgWorker.BackgroundTaskCompleted += delegate(object sender, EventArgs args) { // Signal the waiting NUnit thread that we're ready to move on. manualEvent.Set(); }; //Wait for signal to move on from BackgroundTaskManager.BackgroundTaskCompleted manualEvent.WaitOne(5000, false); Assert.AreEqual(existingCustomerOrdersCount-1, addEditCustomerViewModel.CurrentCustomer.Orders.Count()); #endregion }
public void EditOrderTest() { AddEditCustomerViewModel addEditCustomerViewModel = new AddEditCustomerViewModel(); //Test Command can't run without an order Assert.AreEqual(addEditCustomerViewModel.EditOrderCommand.CanExecute(null), false); addEditCustomerViewModel.CurrentCustomer = CustomerModel.CustomerToCustomerModel(cust); //As setting the AddEditCustomerViewModel.CurrentCustomer causes //a background fetch of all CurrentCustomer.Orders, we need to wait //until that completes to continue ManualResetEvent manualEvent = new ManualResetEvent(false); addEditCustomerViewModel.BgWorker.BackgroundTaskCompleted += delegate(object sender, EventArgs args) { // Signal the waiting NUnit thread that we're ready to move on. manualEvent.Set(); }; //Wait for signal to move on from BackgroundTaskManager.BackgroundTaskCompleted manualEvent.WaitOne(5000, false); addEditCustomerViewModel.CurrentCustomerOrder = addEditCustomerViewModel.CurrentCustomer.Orders.First(); addEditCustomerViewModel.CurrentCustomerOrder.Quantity.DataValue = 1; //Test that EditOrderCommand can now Execute Assert.AreEqual(addEditCustomerViewModel.EditOrderCommand.CanExecute(null), true); //Run the AddOrderCommand TestUIVisualizerService testUIVisualizerService = (TestUIVisualizerService) ViewModelBase.ServiceProvider.Resolve<IUIVisualizerService>(); //Queue up the response we expect for our given TestUIVisualizerService //for a given ICommand/Method call within the test ViewModel testUIVisualizerService.ShowDialogResultResponders.Enqueue (() => { return true; } ); addEditCustomerViewModel.EditOrderCommand.Execute(null); Assert.AreEqual(addEditCustomerViewModel.EditOrderCommand.CommandSucceeded, true); //Clear the TestUIVisualizerService.ShowDialogResultResponders testUIVisualizerService.ShowDialogResultResponders.Clear(); }
[Ignore] //Work in Isolation, need to find out why doesn't work when running all tests public void CheckDeleteCustomer_Fails() { //create some Customers in the Database 1st String cust1String = Guid.NewGuid().ToString(); Assert.Greater(DataService.AddCustomer( GetCustomer(cust1String, "1")), 0); SearchCustomersViewModel searchCustomersVM = new SearchCustomersViewModel(); //should not be able to delete without a current customer Assert.AreEqual(false, searchCustomersVM.DeleteCustomerCommand.CanExecute(null)); FetchCustomer(searchCustomersVM, String.Format("{0}_1", cust1String)); Assert.AreEqual(1, searchCustomersVM.MatchedCustomers.Where( c => c.FirstName.DataValue == searchCustomersVM.CurrentFilterText).Count()); searchCustomersVM.CurrentCustomer = searchCustomersVM.MatchedCustomers[0]; //should be able to delete with a current customer, unless //it is open for edit within the MainWindowViewModel Assert.AreEqual(true, searchCustomersVM.DeleteCustomerCommand.CanExecute(null)); //As we are testing for failure ensure that this Customer up for deletion //is currently being edited within the AddEditCustomerViewModel workspace //within the MainWindowViewModel. //Then try and delete the customer, but can only do it if the customer //is not being edited in MainWindowViewModel, so should fail MainWindowViewModel mainWindowViewModel = new MainWindowViewModel(); AddEditCustomerViewModel addEditCustomerViewModel = new AddEditCustomerViewModel(); addEditCustomerViewModel.CurrentCustomer = searchCustomersVM.MatchedCustomers[0]; mainWindowViewModel.Workspaces.Add(addEditCustomerViewModel); searchCustomersVM.DeleteCustomerCommand.Execute(null); Assert.AreEqual(1, searchCustomersVM.MatchedCustomers.Where( c => c.FirstName.DataValue == searchCustomersVM.CurrentFilterText).Count()); }