예제 #1
0
        public async Task CommandCanAlwaysBeInvokedDirectly_RegardlessWhetherTheCommandCanBeExecutedOrNot()
        {
            int                      number     = 0;
            bool                     canExecute = false;
            IBoundedCommand          command    = new BoundedRelayCommand(() => { number++; return(Task.CompletedTask); }, () => canExecute);
            IBoundedCommand <string> commandT   = new BoundedRelayCommand <string>(_ => { number--; return(Task.CompletedTask); }, _ => canExecute);

            Assert.False(command.CanExecute());
            await command.ExecuteAsync();

            Assert.Equal(1, number);
            Assert.False(commandT.CanExecute("F0"));
            await commandT.ExecuteAsync("F0");

            Assert.Equal(0, number);

            canExecute = true;

            Assert.True(command.CanExecute());
            await command.ExecuteAsync();

            Assert.Equal(1, number);
            Assert.True(commandT.CanExecute("F0"));
            await commandT.ExecuteAsync("F0");

            Assert.Equal(0, number);
        }
예제 #2
0
        public async Task TheCommandCannotBeExecutedIfTheCurrentCountOfOperationsExceedTheMaximumCount()
        {
            TaskCompletionSource tcs  = new();
            TaskCompletionSource tcsT = new();

            IBoundedCommand          command  = new BoundedRelayCommand(() => tcs.Task, 1);
            IBoundedCommand <string> commandT = new BoundedRelayCommand <string>(_ => tcsT.Task, 1);

            Assert.Equal(0, command.CurrentCount);
            Assert.Equal(0, commandT.CurrentCount);
            Assert.True(command.CanExecute());
            Assert.True(commandT.CanExecute("F0"));

            Task operation  = command.ExecuteAsync();
            Task operationT = commandT.ExecuteAsync("F0");

            Assert.Equal(1, command.CurrentCount);
            Assert.Equal(1, commandT.CurrentCount);
            Assert.False(command.CanExecute());
            Assert.False(commandT.CanExecute("F0"));

            tcs.SetResult();
            tcsT.SetResult();
            await operation;
            await operationT;

            Assert.Equal(0, command.CurrentCount);
            Assert.Equal(0, commandT.CurrentCount);
            Assert.True(command.CanExecute());
            Assert.True(commandT.CanExecute("F0"));
        }
예제 #3
0
        public void TheExecutionStatusLogicDeterminesWhetherTheBoundedRelayCommandCanExecuteInItsCurrentState()
        {
            bool                     canExecute = false;
            IBoundedCommand          command    = new BoundedRelayCommand(() => Task.CompletedTask, () => canExecute);
            IBoundedCommand <string> commandT   = new BoundedRelayCommand <string>(_ => Task.CompletedTask, _ => canExecute);

            Assert.False(command.CanExecute());
            Assert.False(commandT.CanExecute("F0"));

            canExecute = true;

            Assert.True(command.CanExecute());
            Assert.True(commandT.CanExecute("F0"));
        }
예제 #4
0
        public async Task CommandCanAlwaysBeInvokedDirectly_RegardlessWhetherTheMaximumCountIsExceededOrNot()
        {
            int number  = 0;
            int numberT = 0;
            TaskCompletionSource     tcs      = new();
            TaskCompletionSource     tcsT     = new();
            IBoundedCommand          command  = new BoundedRelayCommand(() => { number++; return(tcs.Task); }, 1);
            IBoundedCommand <string> commandT = new BoundedRelayCommand <string>(_ => { numberT++; return(tcsT.Task); }, 1);

            Assert.Equal(0, number);
            Assert.Equal(0, numberT);
            Assert.Equal(0, command.CurrentCount);
            Assert.Equal(0, commandT.CurrentCount);
            Assert.True(command.CanExecute());
            Assert.True(commandT.CanExecute("F0"));

            Task operationOne  = command.ExecuteAsync();
            Task operationOneT = commandT.ExecuteAsync("F0");

            Assert.Equal(1, number);
            Assert.Equal(1, numberT);
            Assert.Equal(1, command.CurrentCount);
            Assert.Equal(1, commandT.CurrentCount);
            Assert.False(command.CanExecute());
            Assert.False(commandT.CanExecute("F0"));

            Task operationTwo  = command.ExecuteAsync();
            Task operationTwoT = commandT.ExecuteAsync("F0");

            Assert.Equal(2, number);
            Assert.Equal(2, numberT);
            Assert.Equal(2, command.CurrentCount);
            Assert.Equal(2, commandT.CurrentCount);
            Assert.False(command.CanExecute());
            Assert.False(commandT.CanExecute("F0"));

            tcs.SetResult();
            tcsT.SetResult();
            await operationOne;
            await operationOneT;
            await operationTwo;
            await operationTwoT;

            Assert.Equal(2, number);
            Assert.Equal(2, numberT);
            Assert.Equal(0, command.CurrentCount);
            Assert.Equal(0, commandT.CurrentCount);
            Assert.True(command.CanExecute());
            Assert.True(commandT.CanExecute("F0"));
        }
예제 #5
0
        public async Task ExecuteTheCommandOnlyIfTheCommandCanBeExecuted_ReturnTrueIfTheCommandWasExecuted_OtherwiseFalse_IBoundedCommand()
        {
            int             executeCount = 0;
            bool            isEnabled    = true;
            IBoundedCommand command      = new BoundedRelayCommand(async() => { await Task.Yield(); executeCount++; }, () => isEnabled);

            Assert.True(command.CanExecute());
            Assert.True(await command.TryExecuteAsync());
            Assert.Equal(1, executeCount);

            isEnabled = false;
            Assert.False(command.CanExecute());
            Assert.False(await command.TryExecuteAsync());
            Assert.Equal(1, executeCount);
        }
예제 #6
0
        public async Task Return_TrueIfTheCommandHasBeenExecuted_FalseIfTheCommandHasNotBeenExecuted_IBoundedCommand()
        {
            int  executeCount             = 0;
            bool isEnabled                = true;
            IBoundedCommand <int> command = new BoundedRelayCommand <int>(async integer => { await Task.Yield(); executeCount += integer; }, _ => isEnabled);

            Assert.True(command.CanExecute(9));
            Assert.True(await command.TryExecuteAsync(9));
            Assert.Equal(9, executeCount);

            isEnabled = false;
            Assert.False(command.CanExecute(9));
            Assert.False(await command.TryExecuteAsync(9));
            Assert.Equal(9, executeCount);
        }
예제 #7
0
        public void TheDefaultReturnValueForTheCanExecuteMethodIsTrue()
        {
            IBoundedCommand          command  = new BoundedRelayCommand(() => Task.CompletedTask);
            IBoundedCommand <string> commandT = new BoundedRelayCommand <string>(_ => Task.CompletedTask);

            Assert.True(command.CanExecute());
            Assert.True(commandT.CanExecute("F0"));
        }
예제 #8
0
        public async Task InContrastToTheNonGenericCommandWhichDoesNotSupportDataToBePassed_DataIsUsedByTheStronglyTypedCommand()
        {
            int number = 0;
            IBoundedCommand <int> commandT = new BoundedRelayCommand <int>(addend => { number += addend; return(Task.CompletedTask); }, integer => integer % 2 == 0);

            Assert.False(commandT.CanExecute(1));
            Assert.True(commandT.CanExecute(2));

            Assert.Equal(0, number);
            await commandT.ExecuteAsync(1);

            Assert.Equal(1, number);
            await commandT.ExecuteAsync(2);

            Assert.Equal(3, number);
            await commandT.ExecuteAsync(3);

            Assert.Equal(6, number);
            await commandT.ExecuteAsync(-9);

            Assert.Equal(-3, number);
        }
예제 #9
0
        public async Task OrderOfExecutionForEvents()
        {
            string text  = "";
            string textT = "";

            IBoundedCommand          command  = new BoundedRelayCommand(() => { text += "240,"; return(Task.CompletedTask); }, 1);
            IBoundedCommand <string> commandT = new BoundedRelayCommand <string>(parameter => { textT += parameter + ","; return(Task.CompletedTask); }, 1);

            ((INotifyPropertyChanged)command).PropertyChanged  += (sender, e) => text += command.CurrentCount + ",";
            ((INotifyPropertyChanged)commandT).PropertyChanged += (sender, e) => textT += commandT.CurrentCount + ",";
            command.CanExecuteChanged  += (sender, e) => text += command.CanExecute() + ",";
            commandT.CanExecuteChanged += (sender, e) => textT += commandT.CanExecute("F0") + ",";

            await command.ExecuteAsync();

            await commandT.ExecuteAsync("F_0");

            Assert.Equal("1,False,240,0,True,", text);
            Assert.Equal("1,False,F_0,0,True,", textT);
        }
예제 #10
0
        public async Task RaiseCanExecuteChangedEvent_WhenChangesToTheCurrentCountOccurThatAffectWhetherOrNotTheCommandShouldExecute()
        {
            TaskCompletionSource     tcs      = new();
            TaskCompletionSource     tcsT     = new();
            IBoundedCommand          command  = new BoundedRelayCommand(() => tcs.Task, 2);
            IBoundedCommand <string> commandT = new BoundedRelayCommand <string>(_ => tcsT.Task, 2);

            List <object?>   senders     = new();
            List <object?>   sendersT    = new();
            List <EventArgs> eventArgs   = new();
            List <EventArgs> eventArgsT  = new();
            List <bool>      canExecute  = new();
            List <bool>      canExecuteT = new();

            void OnCanExecuteChanged(object?sender, EventArgs e)
            {
                senders.Add(sender);
                eventArgs.Add(e);
                canExecute.Add(command.CanExecute());
            }

            void OnCanExecuteChangedT(object?sender, EventArgs e)
            {
                sendersT.Add(sender);
                eventArgsT.Add(e);
                canExecuteT.Add(commandT.CanExecute("F0"));
            }

            command.CanExecuteChanged  += OnCanExecuteChanged;
            commandT.CanExecuteChanged += OnCanExecuteChangedT;

            Task operation1  = command.ExecuteAsync();
            Task operation1T = commandT.ExecuteAsync("F0");

            Assert.Equal(new object[] { }, senders);
            Assert.Equal(new object[] { }, sendersT);
            Assert.Equal(new EventArgs[] { }, eventArgs);
            Assert.Equal(new EventArgs[] { }, eventArgsT);
            Assert.Equal(new bool[] { }, canExecute);
            Assert.Equal(new bool[] { }, canExecuteT);

            Task operation2  = command.ExecuteAsync();
            Task operation2T = commandT.ExecuteAsync("F0");

            Assert.Equal(new object[] { command }, senders);
            Assert.Equal(new object[] { commandT }, sendersT);
            Assert.Equal(new EventArgs[] { EventArgs.Empty }, eventArgs);
            Assert.Equal(new EventArgs[] { EventArgs.Empty }, eventArgsT);
            Assert.Equal(new bool[] { false }, canExecute);
            Assert.Equal(new bool[] { false }, canExecuteT);

            Task operation3  = command.ExecuteAsync();
            Task operation3T = commandT.ExecuteAsync("F0");

            Assert.Equal(new object[] { command }, senders);
            Assert.Equal(new object[] { commandT }, sendersT);
            Assert.Equal(new EventArgs[] { EventArgs.Empty }, eventArgs);
            Assert.Equal(new EventArgs[] { EventArgs.Empty }, eventArgsT);
            Assert.Equal(new bool[] { false }, canExecute);
            Assert.Equal(new bool[] { false }, canExecuteT);

            tcs.SetResult();
            tcsT.SetResult();
            await operation1;
            await operation1T;
            await operation2;
            await operation2T;
            await operation3;
            await operation3T;

            Assert.Equal(new object[] { command, command }, senders);
            Assert.Equal(new object[] { commandT, commandT }, sendersT);
            Assert.Equal(new EventArgs[] { EventArgs.Empty, EventArgs.Empty }, eventArgs);
            Assert.Equal(new EventArgs[] { EventArgs.Empty, EventArgs.Empty }, eventArgsT);
            Assert.Equal(new bool[] { false, true }, canExecute);
            Assert.Equal(new bool[] { false, true }, canExecuteT);
        }
예제 #11
0
        public async Task RaiseCanExecuteChangedEvent()
        {
            TaskCompletionSource     tcs      = new();
            TaskCompletionSource     tcsT     = new();
            IBoundedCommand          command  = new BoundedRelayCommand(() => tcs.Task, 1);
            IBoundedCommand <string> commandT = new BoundedRelayCommand <string>(_ => tcsT.Task, 1);

            List <object?>   senders     = new();
            List <object?>   sendersT    = new();
            List <EventArgs> eventArgs   = new();
            List <EventArgs> eventArgsT  = new();
            List <bool>      canExecute  = new();
            List <bool>      canExecuteT = new();

            void OnCanExecuteChanged(object?sender, EventArgs e)
            {
                senders.Add(sender);
                eventArgs.Add(e);
                canExecute.Add(command.CanExecute());
            }

            void OnCanExecuteChangedT(object?sender, EventArgs e)
            {
                sendersT.Add(sender);
                eventArgsT.Add(e);
                canExecuteT.Add(commandT.CanExecute("F0"));
            }

            command.CanExecuteChanged  += OnCanExecuteChanged;
            commandT.CanExecuteChanged += OnCanExecuteChangedT;

            Task operation  = command.ExecuteAsync();
            Task operationT = commandT.ExecuteAsync("F0");

            Assert.Equal(new object[] { command }, senders);
            Assert.Equal(new object[] { commandT }, sendersT);
            Assert.Equal(new EventArgs[] { EventArgs.Empty }, eventArgs);
            Assert.Equal(new EventArgs[] { EventArgs.Empty }, eventArgsT);
            Assert.Equal(new bool[] { false }, canExecute);
            Assert.Equal(new bool[] { false }, canExecuteT);

            tcs.SetResult();
            tcsT.SetResult();
            await operation;
            await operationT;

            Assert.Equal(new object[] { command, command }, senders);
            Assert.Equal(new object[] { commandT, commandT }, sendersT);
            Assert.Equal(new EventArgs[] { EventArgs.Empty, EventArgs.Empty }, eventArgs);
            Assert.Equal(new EventArgs[] { EventArgs.Empty, EventArgs.Empty }, eventArgsT);
            Assert.Equal(new bool[] { false, true }, canExecute);
            Assert.Equal(new bool[] { false, true }, canExecuteT);

            command.CanExecuteChanged  -= OnCanExecuteChanged;
            commandT.CanExecuteChanged -= OnCanExecuteChangedT;

            await command.ExecuteAsync();

            await commandT.ExecuteAsync("F0");

            Assert.Equal(new object[] { command, command }, senders);
            Assert.Equal(new object[] { commandT, commandT }, sendersT);
            Assert.Equal(new EventArgs[] { EventArgs.Empty, EventArgs.Empty }, eventArgs);
            Assert.Equal(new EventArgs[] { EventArgs.Empty, EventArgs.Empty }, eventArgsT);
            Assert.Equal(new bool[] { false, true }, canExecute);
            Assert.Equal(new bool[] { false, true }, canExecuteT);
        }