Ejemplo n.º 1
0
        public void ActionCommands_FireAsExpected()
        {
            // arrange
            var fireBasic = new MockActionCommand();
            var fireParameter = new MockActionCommand();

            ICommand basicCommand = new ActionCommand(fireBasic.ExecuteNone, fireBasic.CanExecute);
            ICommand valueTypeCommand = new ActionCommand<int>(fireParameter.ExecuteParam, fireParameter.CanExecuteParam);

            // act
            basicCommand.Execute(null);
            basicCommand.Execute("n/a input");
            valueTypeCommand.Execute(2);

            Assert.IsTrue(basicCommand.CanExecute(null));
            Assert.IsTrue(basicCommand.CanExecute("any input ignored"));
            Assert.IsTrue(valueTypeCommand.CanExecute(1));

            // assert
            Assert.IsTrue(fireBasic.ExecuteFired);
            Assert.IsTrue(fireBasic.TestFired);

            Assert.IsTrue(fireParameter.ExecuteFired);
            Assert.IsTrue(fireParameter.TestFired);
            Assert.AreEqual(2, fireParameter.ObjectsPassedIn.Count);
            Assert.AreEqual(3, fireParameter.ObjectsPassedIn.Sum());
        }
		public void Execute_and_Parameters() {
			const Int32 initial = Int32.MinValue;
			var value = initial;
			var command = new ActionCommand<Int32>(x => value = x);

			Assert.IsTrue(command.CanExecute(0));
			Assert.IsTrue(command.CanExecute(1));

			Assert.AreEqual(initial, value);
			command.Execute(1);
			Assert.AreEqual(1, value);

			command.Execute(0);
			Assert.AreEqual(0, value);
		}
Ejemplo n.º 3
0
        public void ActionCommands_InvalidInput_RespondsAsExpected()
        {
            // arrange
            var fireBasic = new MockActionCommand();
            var fireParameter = new MockActionCommand();

            ICommand parameterCommandInt32 = new ActionCommand<int>(fireParameter.ExecuteParam, fireParameter.CanExecuteParam);
            ICommand parameterCommandObject = new ActionCommand<object>((o) => { });
            ICommand parameterCommandObjectFunc = new ActionCommand<object>((o) => { }, (o) => { return true; });

            // assert
            try
            {
                parameterCommandInt32.CanExecute(null);
                Assert.Fail("Expected error");
            }
            catch (NullReferenceException) { }
            try
            {
                parameterCommandInt32.Execute(null);
                Assert.Fail("Expected error");
            }
            catch (NullReferenceException) { }

            parameterCommandObject.Execute(null);
            parameterCommandObjectFunc.Execute(null);

            Assert.IsTrue(parameterCommandObject.CanExecute(null));
            Assert.IsTrue(parameterCommandObjectFunc.CanExecute(null));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Shows the specified on ok.
        /// </summary>
        /// <param name="onOk">The on ok.</param>
        /// <param name="onCancel">The on cancel.</param>
        /// <returns>PopupBuilder.</returns>
        public virtual PopupBuilder Show(Action onOk = null, Func<bool> onCancel = null)
        {
            OkCommand = null;
            _onCancel = onCancel;

            _customViewModelList = new ObservableCollection<INotificationViewModel>();

            if (_panel == null)
            {
                if (Application.Current.RootVisual == null) return this; //Runnig from Unit tests - no root visual
                _panel = PopupHelper.FindTopLevelPanel(Application.Current.RootVisual);
            }

            if (_position == PopupPosition.Right || _position == PopupPosition.Top)
                _parentPanel = GetStackPanel();
            else
                _parentPanel = GetGridPanel();

            if (_isModal)
            {
                _modalBorder = new Border
                                   {
                                       Opacity = 0.3,
                                       Background = new SolidColorBrush(Colors.LightGray),
                                       HorizontalAlignment = HorizontalAlignment.Stretch,
                                       VerticalAlignment = VerticalAlignment.Stretch,
                                   };
                _modalBorder.SetValue(Canvas.ZIndexProperty, ThePopupFactory.Value.NextZIndex());

                _parentPanel.Children.Add(_modalBorder);
            }

            if (_customViewModel != null)
            {
                _customViewModelList.Add(_customViewModel);
                if (_customViewModel is ISelectableList && onOk != null)
                {
                    (_customViewModel as ISelectableList).OnOK += PopupBuilderOnOK;
                    _popupBuilderOnOK = onOk;
                }
            }
            else
            {
                var commandList = CommandListFactory.CreateExport().Value;
                commandList.NotificationMessage = _message;

                _customViewModelList.Add(commandList);
            }

            _popupView = AddPopupControl(_parentPanel);

            if (CommandListVM != null) _customViewModelList.Add(CommandListVM);

            AddVisualStates(_popupView, _parentPanel);

            if (onOk != null)
            {
                CommandListVM = CommandListFactory.CreateExport().Value;
                if (_customViewModel != null)
                {
                    _customViewModel.InvokeOk = () => OkCommand.Execute(null);
                }

                OkCommand = new ActionCommand<object>(
                    o =>
                    {
                        if(ThePopupFactory.Value.Popups.Any(x=>x._isChild))
                            return;

                        HidePopup();
                        onOk();
                    },
                    o => !CommandListVM.HasErrors && (_customViewModel == null || !_customViewModel.HasErrors));

                var cancelCommand = onCancel == null ? new ActionCommand<object>(o => { }) : new ActionCommand<object>(o => CancelPopup(onCancel));

                AddCommandRange(
                    new List<ITitledCommand>
                        {
                            new TitledCommand(OkCommand, LanguageService.Translate("Label_OK"), string.Empty),
                            new TitledCommand(cancelCommand, LanguageService.Translate("Cmd_Cancel"), string.Empty)
                        });

                CommandListVM.Closed += CustomViewModelClosed;
            }

            if (CommandListVM != null)
            {
                CommandListVM.Orientation = _position == PopupPosition.Right ? Orientation.Vertical : Orientation.Horizontal;
            }

            ShowPopup();
            return this;
        }
Ejemplo n.º 5
0
        public void Execute()
        {
            var random = new Random();
            int parameter = random.Next();

            var executed = false;

            var command = new ActionCommand(() => executed = true);

            command.Execute(parameter);

            Assert.IsTrue(executed);
        }
Ejemplo n.º 6
0
        public void Execute_Generic()
        {
            var random = new Random();
            int parameter = random.Next();

            int result = -1;

            var command = new ActionCommand<int>(i => result = i);

            command.Execute(parameter);

            Assert.AreEqual(parameter, result);
        }