예제 #1
0
        public void CancelCommand_CommandIsNew_CommandIsCancled()
        {
            var fakeFilterManager = A.Fake <IFilterManager>();
            var processor         = new CommandProcessor(null, fakeFilterManager);
            var command           = new TestCommand(CommandState.New);

            processor.CancelCommand(command);

            Assert.AreEqual(CommandState.Canceled, command.CurrentState);
        }
예제 #2
0
        public void CancelCommand_CommandIsPending_CommandIsCancled()
        {
            var resetEvent = new ManualResetEventSlim(false);

            var fakeFilterManager = A.Fake <IFilterManager>();

            A.CallTo(() => fakeFilterManager.Process(A <CommandBase> .Ignored)).Returns(false);
            var processor = new CommandProcessor(null, fakeFilterManager);
            var command   = new TestCommand(CommandState.New, shouldFailIfFiltered: false,
// ReSharper disable RedundantArgumentName
                                            //We want to keep this verbose to clarity
                                            startRequestAction: (s, e) => resetEvent.Set());

// ReSharper restore RedundantArgumentName
            processor.PublishCommand(command);
            resetEvent.Wait();

            processor.CancelCommand(command);

            Assert.AreEqual(CommandState.Canceled, command.CurrentState);
        }
예제 #3
0
        public void CancelCommand_CommandIsBlocked_CommandIsCancled()
        {
            var resetEvent = new ManualResetEventSlim(false);

            var fakeFilterManager = A.Fake <IFilterManager>();

            A.CallTo(() => fakeFilterManager.Process(A <CommandBase> .Ignored)).Returns(true);
            var processor = new CommandProcessor(null, fakeFilterManager);
            var command   = new TestCommand(CommandState.New, blockCanExecute: true);

            command.RegisterForStateChange(Observer.Create <CommandState>(b =>
            {
                if (b == CommandState.Blocked)
                {
                    resetEvent.Set();
                }
            }));
            processor.PublishCommand(command);
            resetEvent.Wait();

            processor.CancelCommand(command);

            Assert.AreEqual(CommandState.Canceled, command.CurrentState);
        }
예제 #4
0
        public MainViewModel()
        {
            Filters           = new ObservableCollection <IFilter>();
            SendSignalCommand = new DelegateCommand <string>(s =>
            {
                if (string.IsNullOrEmpty(SignalValue))
                {
                    callMe.OnNext(new DeviceResult {
                        CommandId = s
                    });
                }
                else
                {
                    callMe.OnNext(new DeviceResult <string>
                    {
                        CommandId = s,
                        Input     = SignalValue
                    });
                }
            }
                                                             );

            _types = Assembly.GetExecutingAssembly().GetTypes();
            _additionals[typeof(ConnectCommand)] =
                c =>
            {
                CommandBase <bool> command = c as CommandBase <bool>;
                command.CompleteAction = com =>
                {
                    CommandBase <bool> cast = com as CommandBase <bool>;
                    if (cast.ReturnValue)
                    {
                        FilterManager.RemoveFilter(_notConnectedFilter);
                    }
                };
                currentConnectCommand = command;
            };
            _additionals[typeof(AlertsCommand)] =
                c =>
            {
                CommandBase <String> com = c as CommandBase <string>;
                com.Subscribe(
                    Observer.Create <ICommandResponse <string> >(
                        x => AddMessage(x.Sender.ToString() + " Got result " + x.Value.ToString()),
                        ex => AddMessage(ex.Source + " Got Error: " + ex.Message),
                        () => { }));
                currentAlertCommand = c;
            };

            _CanExecutes["AlertsCommand"] = new Func <string, bool>(s => CanConnect);

            CreateCommandCommand = new DelegateCommand <string>(CreateACommand, s =>
            {
                if (_CanExecutes.ContainsKey(s))
                {
                    return(_CanExecutes[s](s));
                }
                return(true);
            });

            ThrowAlertCommand =
                new DelegateCommand <string>(
                    s => callMe.OnNext(new DeviceResult <string> {
                Input = s, CommandId = currentAlertCommand.CommandId
            }));

            Commands = new ObservableCollection <IProcessedCommand>();
            Messages = new ObservableCollection <Message>();

            FilterManager = new FilterManager();
            FilterManager.ItemsAdded.Subscribe(f => Filters.Add(f));
            FilterManager.ItemsRemoved.Subscribe(f => Filters.Remove(f));
            FilterManager.AddFilter(_notConnectedFilter);
            commandFactory = new CommandFactory(FilterManager);
            commandFactory.OnCreateCommand = new Action <CommandBase>(c => Commands.Add(c));

            CommandProcessor = new CommandProcessor(callMe, FilterManager);
            CommandProcessor.RegisterForCompletedCommands(
                Observer.Create <CommandBase>(c => AddMessage(c.ToString() + "  is Completed")));

            SendConnectCommand = new DelegateCommand <bool?>(b =>
                                                             callMe.OnNext(new DeviceResult <bool>
            {
                Input     = b.Value,
                CommandId =
                    currentConnectCommand.CommandId
            }));


            callMe.OnNext(new ProcessorInput());

            ReleaseBlockedCommand = new DelegateCommand <string>(s => CommandProcessor.RerunBlockedCommand((CommandBase)
                                                                                                           Commands.First(c => c.CommandId == s)));
            CancelCommandCommand = new DelegateCommand <string>(s => CommandProcessor.CancelCommand((CommandBase)
                                                                                                    Commands.First(c => c.CommandId == s)));

            CreateSequnceCommand = new DelegateCommand(() =>
            {
                SequenceCommand c1 = new SequenceCommand(this, "AAA");
                SequenceCommand c2 = new SequenceCommand(this, "BBB");
                SequenceCommand c3 = new SequenceCommand(this, "CCC");
                SequenceCommand c4 = new SequenceCommand(this, "DDD");
                Commands.Add(c1);
                Commands.Add(c2);
                Commands.Add(c3);
                Commands.Add(c4);
                subscriptions.AddRange(
                    CommandProcessor.PublishOrderedCommands(
                        new[] { c1, c2, c3, c4 },
                        new[] { MyObserver, MyObserver, MyObserver, MyObserver }));
            });
        }
        public void CancelCommand_CommandIsBlocked_CommandIsCancled()
        {
            var resetEvent = new ManualResetEventSlim(false);

            var fakeFilterManager = A.Fake<IFilterManager>();
            A.CallTo(() => fakeFilterManager.Process(A<CommandBase>.Ignored)).Returns(true);
            var processor = new CommandProcessor(null, fakeFilterManager);
            var command = new TestCommand(CommandState.New, blockCanExecute:true);

            command.RegisterForStateChange(Observer.Create<CommandState>(b =>
            {
                if (b == CommandState.Blocked)
                {
                    resetEvent.Set();
                }
            }));
            processor.PublishCommand(command);
            resetEvent.Wait();

            processor.CancelCommand(command);

            Assert.AreEqual(CommandState.Canceled, command.CurrentState);
        }
        public void CancelCommand_CommandIsPending_CommandIsCancled()
        {
            var resetEvent = new ManualResetEventSlim(false);

            var fakeFilterManager = A.Fake<IFilterManager>();
            A.CallTo(() => fakeFilterManager.Process(A<CommandBase>.Ignored)).Returns(false);
            var processor = new CommandProcessor(null, fakeFilterManager);
            var command = new TestCommand(CommandState.New, shouldFailIfFiltered: false,
            // ReSharper disable RedundantArgumentName
                //We want to keep this verbose to clarity
                                         startRequestAction: (s, e) => resetEvent.Set());
            // ReSharper restore RedundantArgumentName
            processor.PublishCommand(command);
            resetEvent.Wait();

            processor.CancelCommand(command);

            Assert.AreEqual(CommandState.Canceled, command.CurrentState);
        }
        public void CancelCommand_CommandIsNew_CommandIsCancled()
        {
            var fakeFilterManager = A.Fake<IFilterManager>();
            var processor = new CommandProcessor(null, fakeFilterManager);
            var command = new TestCommand(CommandState.New);

            processor.CancelCommand(command);

            Assert.AreEqual(CommandState.Canceled, command.CurrentState);
        }