示例#1
0
        static IEnumerable <CommandBaseTests.TestArgs <AsyncCommand <T> > > _ExecutionCanceled <T>()
        {
            var counter = new CommandBaseTests.CommandExecutionCounter();

            var reentrancyContainer = new CommandBaseTests.ReentrancyContainer <AsyncCommand <T> >();

            async Task Execute(T arg)
            {
                await Task.Yield();

                reentrancyContainer.Command.RunningExecution.CancelCommand.Execute();

                throw new OperationCanceledException();
            }

            async Task ExecuteWithCancellationToken(T arg, CancellationToken cancellationToken)
            {
                try
                {
                    await Execute(arg);
                }
                finally
                {
                    if (reentrancyContainer.IsCancelCommandEnabled)
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            counter.CancellationRequest++;
                        }
                    }
                    else
                    {
                        if (!cancellationToken.IsCancellationRequested)
                        {
                            counter.CancellationRequest++;
                        }
                    }
                }
            }

            async Task ExecuteWithController(T arg, CommandExecutionController controller, CancellationToken cancellationToken)
            {
                await Task.Yield();

                controller.ReportProgress(0.4f);

                await ExecuteWithCancellationToken(arg, cancellationToken);
            }

            foreach (var args in GetTestArgs(reentrancyContainer, Execute, ExecuteWithCancellationToken, ExecuteWithController))
            {
                yield return(args);
            }

            Assert.AreEqual(reentrancyContainer.ExecutionCount / 3 * 2, counter.CancellationRequest);
        }
示例#2
0
        static IEnumerable <CommandBaseTests.TestArgs <Command> > _ExecutionCanceled()
        {
            var counter = new CommandBaseTests.CommandExecutionCounter();

            var reentrancyContainer = new CommandBaseTests.ReentrancyContainer <Command>();

            void Execute()
            {
                reentrancyContainer.Command.RunningExecution.CancelCommand.Execute();

                throw new OperationCanceledException();
            }

            void ExecuteWithCancellationToken(CancellationToken cancellationToken)
            {
                try
                {
                    Execute();
                }
                finally
                {
                    if (reentrancyContainer.IsCancelCommandEnabled)
                    {
                        if (cancellationToken.IsCancellationRequested)
                        {
                            counter.CancellationRequest++;
                        }
                    }
                    else
                    {
                        if (!cancellationToken.IsCancellationRequested)
                        {
                            counter.CancellationRequest++;
                        }
                    }
                }
            }

            void ExecuteWithController(CommandExecutionController controller, CancellationToken cancellationToken)
            {
                controller.ReportProgress(0.4f);

                ExecuteWithCancellationToken(cancellationToken);
            }

            foreach (var args in GetTestArgs(reentrancyContainer, Execute, ExecuteWithCancellationToken, ExecuteWithController))
            {
                yield return(args);
            }

            Assert.AreEqual(reentrancyContainer.ExecutionCount / 3 * 2, counter.CancellationRequest);
        }
示例#3
0
        static IEnumerable <CommandBaseTests.TestArgs <AsyncCommand <T> > > _ExecutionSuccessful <T>()
        {
            var counter = new CommandBaseTests.CommandExecutionCounter();

            var reentrancyContainer = new CommandBaseTests.ReentrancyContainer <AsyncCommand <T> >();

            async Task Execute(T arg)
            {
                await Task.Yield();

                counter.Execution++;

                if (reentrancyContainer.Command.RunningExecution != null)
                {
                    counter.RunningExecution++;

                    if (reentrancyContainer.Command.RunningExecution.CancelCommand != null)
                    {
                        counter.CancelCommand++;

                        if (reentrancyContainer.Command.RunningExecution.CancelCommand.CanExecute() == reentrancyContainer.IsCancelCommandEnabled)
                        {
                            counter.IsCancelCommandEnabled++;
                        }

                        if (Math.Abs(reentrancyContainer.Command.RunningExecution.Progress) < float.Epsilon)
                        {
                            counter.Progress++;
                        }
                    }
                }

                if (!reentrancyContainer.Command.CanExecute(default(T)))
                {
                    counter.CanExecuteWhileExecuting++;
                }

                // nested execution
                await reentrancyContainer.Command.Execute(arg);
            }

            async Task ExecuteWithCancellationToken(T arg, CancellationToken cancellationToken)
            {
                await Task.Yield();

                if (cancellationToken.CanBeCanceled)
                {
                    counter.CanBeCanceled++;
                }
                if (!cancellationToken.IsCancellationRequested)
                {
                    counter.CancellationRequest++;
                }

                await Execute(arg);
            }

            async Task ExecuteWithController(T arg, CommandExecutionController controller, CancellationToken cancellationToken)
            {
                await Task.Yield();

                if (controller != null)
                {
                    counter.Controller++;
                }

                if (controller.Execution == reentrancyContainer.Command.RunningExecution)
                {
                    counter.SameRunningExecution++;
                }

                await ExecuteWithCancellationToken(arg, cancellationToken);

                ExceptionAssert.Throws <ArgumentOutOfRangeException>(() => controller.ReportProgress(-0.1f), "value");
                ExceptionAssert.Throws <ArgumentOutOfRangeException>(() => controller.ReportProgress(1.1f), "value");

                controller.ReportProgress(0.3f);
                if (Math.Abs(controller.Execution.Progress - 0.3f) < float.Epsilon)
                {
                    counter.ProgressReporting++;
                }

                ((IProgress <float>)controller).Report(-0.1f);
                ((IProgress <float>)controller).Report(1.1f);
                ((IProgress <float>)controller).Report(0.35f);
                if (Math.Abs(controller.Execution.Progress - 0.35f) < float.Epsilon)
                {
                    counter.ProgressReportingAsIProgress++;
                }

                controller.DisableCancelCommand();
                if (!controller.Execution.CancelCommand.CanExecute())
                {
                    counter.DisabledCancelCommand++;
                }

                reentrancyContainer.CapturedController = controller;
            }

            foreach (var args in GetTestArgs(reentrancyContainer, Execute, ExecuteWithCancellationToken, ExecuteWithController))
            {
                yield return(args);
            }

            Assert.AreEqual(reentrancyContainer.ExecutionCount, counter.Execution);
            Assert.AreEqual(counter.Execution, counter.RunningExecution);
            Assert.AreEqual(counter.Execution, counter.CancelCommand);
            Assert.AreEqual(counter.Execution, counter.IsCancelCommandEnabled);
            Assert.AreEqual(counter.Execution, counter.Progress);
            Assert.AreEqual(counter.Execution, counter.CanExecuteWhileExecuting);

            Assert.AreEqual(reentrancyContainer.ExecutionCount / 3 * 2, counter.CanBeCanceled);
            Assert.AreEqual(counter.CanBeCanceled, counter.CancellationRequest);

            Assert.AreEqual(reentrancyContainer.ExecutionCount / 3, counter.Controller);
            Assert.AreEqual(counter.Controller, counter.SameRunningExecution);
            Assert.AreEqual(counter.Controller, counter.ProgressReporting);
            Assert.AreEqual(counter.Controller, counter.ProgressReportingAsIProgress);
            Assert.AreEqual(counter.Controller, counter.DisabledCancelCommand);
        }