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);


        }
        public void SaveOrderCommandTest()
        {
            AddEditOrderViewModel addEditOrderViewModel =
                new AddEditOrderViewModel();
            
            //Test Command can't run without an order
            Assert.AreEqual(addEditOrderViewModel.SaveOrderCommand.CanExecute(null), false);

            #region AddMode
            addEditOrderViewModel.CurrentViewMode = ViewMode.AddMode;


            addEditOrderViewModel.CurrentCustomer = CustomerModel.CustomerToCustomerModel(cust);
            addEditOrderViewModel.CurrentCustomerOrder = OrderModel.OrderToOrderModel(ord);

            //test Save Command can run
            Assert.AreEqual(addEditOrderViewModel.SaveOrderCommand.CanExecute(null), true);

            //Execute SaveCommand
            addEditOrderViewModel.SaveOrderCommand.Execute(null);
            Assert.Greater(addEditOrderViewModel.CurrentCustomerOrder.OrderId.DataValue, 0);


            #endregion

            #region EditMode
            addEditOrderViewModel.CurrentViewMode = ViewMode.EditMode;


            addEditOrderViewModel.CurrentCustomer = CustomerModel.CustomerToCustomerModel(cust);

            Order o = DataAccess.DataService.FetchAllOrders(cust.CustomerId).First();

            addEditOrderViewModel.CurrentCustomerOrder = OrderModel.OrderToOrderModel(o);

            //test Save Command can run
            Assert.AreEqual(addEditOrderViewModel.SaveOrderCommand.CanExecute(null), true);

            //Execute SaveCommand
            addEditOrderViewModel.SaveOrderCommand.Execute(null);
            Assert.AreEqual(addEditOrderViewModel.SaveOrderCommand.CommandSucceeded, true);


            #endregion


        }
Exemple #3
0
        public AddEditCustomerViewModel()
        {
            this.DisplayName = "Add Customer";

            #region Obtain Services
            try
            {
                messageBoxService   = Resolve <IMessageBoxService>();
                uiVisualizerService = Resolve <IUIVisualizerService>();
            }
            catch
            {
                Logger.Error("Error resolving services");
                throw new ApplicationException("Error resolving services");
            }
            #endregion

            #region Create Commands
            //Create save customer Command
            saveCustomerCommand = new SimpleCommand
            {
                CanExecuteDelegate = x => CanExecuteSaveCustomerCommand,
                ExecuteDelegate    = x => ExecuteSaveCustomerCommand()
            };
            //Create edit customer Command
            editCustomerCommand = new SimpleCommand
            {
                CanExecuteDelegate = x => CanExecuteEditCustomerCommand,
                ExecuteDelegate    = x => ExecuteEditCustomerCommand()
            };
            //Create cancel customer Command
            cancelCustomerCommand = new SimpleCommand
            {
                CanExecuteDelegate = x => CanExecuteCancelCustomerCommand,
                ExecuteDelegate    = x => ExecuteCancelCustomerCommand()
            };
            //Add Order to customer Command
            addOrderCommand = new SimpleCommand
            {
                CanExecuteDelegate = x => CanExecuteAddOrderCommand,
                ExecuteDelegate    = x => ExecuteAddOrderCommand()
            };
            //Edit Order to customer Command
            editOrderCommand = new SimpleCommand
            {
                CanExecuteDelegate = x => CanExecuteEditOrderCommand,
                ExecuteDelegate    = x => ExecuteEditOrderCommand()
            };
            //Delete Order Command
            deleteOrderCommand = new SimpleCommand
            {
                CanExecuteDelegate = x => CanExecuteDeleteOrderCommand,
                ExecuteDelegate    = x => ExecuteDeleteOrderCommand()
            };

            addEditOrderVM = new AddEditOrderViewModel();

            //setup background worker
            SetUpBackgroundWorker();

            #endregion
        }
        public AddEditCustomerViewModel()
        {
            this.DisplayName = "Add Customer";

            #region Obtain Services
            try
            {
                messageBoxService = Resolve<IMessageBoxService>();
                uiVisualizerService = Resolve<IUIVisualizerService>();
            }
            catch
            {
                Logger.Error( "Error resolving services");
                throw new ApplicationException("Error resolving services");
            }
            #endregion

            #region Create Commands
            //Create save customer Command
            saveCustomerCommand = new SimpleCommand
            {
                CanExecuteDelegate = x => CanExecuteSaveCustomerCommand,
                ExecuteDelegate = x => ExecuteSaveCustomerCommand()
            };
            //Create edit customer Command
            editCustomerCommand = new SimpleCommand
            {
                CanExecuteDelegate = x => CanExecuteEditCustomerCommand,
                ExecuteDelegate = x => ExecuteEditCustomerCommand()
            };
            //Create cancel customer Command
            cancelCustomerCommand = new SimpleCommand
            {
                CanExecuteDelegate = x => CanExecuteCancelCustomerCommand,
                ExecuteDelegate = x => ExecuteCancelCustomerCommand()
            };
            //Add Order to customer Command
            addOrderCommand = new SimpleCommand
            {
                CanExecuteDelegate = x => CanExecuteAddOrderCommand,
                ExecuteDelegate = x => ExecuteAddOrderCommand()
            };
            //Edit Order to customer Command
            editOrderCommand = new SimpleCommand
            {
                CanExecuteDelegate = x => CanExecuteEditOrderCommand,
                ExecuteDelegate = x => ExecuteEditOrderCommand()
            };
            //Delete Order Command
            deleteOrderCommand = new SimpleCommand
            {
                CanExecuteDelegate = x => CanExecuteDeleteOrderCommand,
                ExecuteDelegate = x => ExecuteDeleteOrderCommand()
            };

            addEditOrderVM = new AddEditOrderViewModel();

            //setup background worker
            SetUpBackgroundWorker();

            #endregion
        }
        public void EditOrderCommandTest(AddEditOrderViewModel addEditOrderViewModel)
        {


            //Test that EditOrderCommand can not execute with no current order
            addEditOrderViewModel.CurrentCustomerOrder = null;
            Assert.AreEqual(addEditOrderViewModel.EditOrderCommand.CanExecute(null), false);


            //now give it an order, and check that EditOrderCommand can't run until its in correct mode
            addEditOrderViewModel.CurrentCustomerOrder = OrderModel.OrderToOrderModel(ord);

            addEditOrderViewModel.CurrentViewMode = ViewMode.EditMode;
            Assert.AreEqual(addEditOrderViewModel.EditOrderCommand.CanExecute(null), false);


            //now allow the EditOrderCommand to run by placing it in the correct mode
            addEditOrderViewModel.CurrentViewMode = ViewMode.ViewOnlyMode;
            Assert.AreEqual(addEditOrderViewModel.EditOrderCommand.CanExecute(null), true);

            //execute the EditOrderCommand
            addEditOrderViewModel.EditOrderCommand.Execute(null);
            Assert.AreEqual(addEditOrderViewModel.CurrentViewMode, ViewMode.EditMode);
            Assert.AreEqual(addEditOrderViewModel.EditOrderCommand.CommandSucceeded, true);



            

        } 
        public void CancelOrderCommandTest()
        {
            AddEditOrderViewModel addEditOrderViewModel =
                new AddEditOrderViewModel();


            //test the edit command first, which allows us to put 
            //order into edit mode
            EditOrderCommandTest(addEditOrderViewModel);

            //so now make an edit to the order, say change Quantity
            Int32 editQty = 9999;
            ord.Quantity = editQty;

            #region CancelOrderCommandTests

            //check that CancelOrderCommand can't run until its in correct mode
            addEditOrderViewModel.CurrentViewMode = ViewMode.ViewOnlyMode;
            Assert.AreEqual(addEditOrderViewModel.CancelOrderCommand.CanExecute(null), false);


            //now allow the CancelOrderCommand to run by placing it in the correct mode
            addEditOrderViewModel.CurrentViewMode = ViewMode.EditMode;
            Assert.AreEqual(addEditOrderViewModel.CancelOrderCommand.CanExecute(null), true);

            //execute the CancelOrderCommand
            addEditOrderViewModel.CancelOrderCommand.Execute(null);
            Assert.AreNotEqual(addEditOrderViewModel.CurrentCustomerOrder.Quantity.DataValue, editQty);
            Assert.AreEqual(addEditOrderViewModel.CancelOrderCommand.CommandSucceeded, true);



            //Test that CancelOrderCommand can not execute with out a current order
            addEditOrderViewModel.CurrentCustomerOrder = null;
            Assert.AreEqual(addEditOrderViewModel.CancelOrderCommand.CanExecute(null), false);
            #endregion

        }