public void It_Raises_CanExecute_When_Property_Changes()
        {
            var canExecute   = true;
            var property     = new DynamicProperty <bool>(DefaultPropertyName, false);
            var testStrategy = new TestCommandStrategy();

            var strategy = new CanExecuteCommandStrategy(property)
            {
                InnerStrategy = testStrategy
            };

            var command = new DynamicCommand(DefaultCommandName, strategy);

            command.CanExecuteChanged += OnCanExecuteChanged;

            canExecute = command.CanExecute(null);
            canExecute.Should().BeFalse();

            property.Value = true;
            canExecute.Should().BeTrue();

            property.Value = false;
            canExecute.Should().BeFalse();

            void OnCanExecuteChanged(object sender, EventArgs e)
            {
                canExecute = command.CanExecute(null);
            }
        }
示例#2
0
        public async Task It_Disables_While_Executing()
        {
            var taskCompletionSource = new TaskCompletionSource <object>();

            var testStrategy = new TestCommandStrategy(
                onExecute: (_, __, ___) => taskCompletionSource.Task
                );

            var strategy = new DisableWhileExecutingCommandStrategy()
            {
                InnerStrategy = testStrategy
            };

            var command = new DynamicCommand(DefaultCommandName, strategy);

            command.CanExecuteChanged += OnCanExecuteChanged;

            var canExecute = command.CanExecute(null);

            // The command should be enabled
            canExecute.Should().BeTrue();

            // We execute the command
            var commandExecution = command.Execute();

            // The command should be disabled
            canExecute.Should().BeFalse();

            // The command completes
            taskCompletionSource.TrySetResult(null);

            await commandExecution;

            // The command should be enabled
            canExecute.Should().BeTrue();

            void OnCanExecuteChanged(object sender, EventArgs e)
            {
                canExecute = command.CanExecute(null);
            }
        }