public void ExecuteT_IViewModelBase_CalledTwice_FiresOnceIfBusy() { int times = 0; async Task MockTask(string text) { await Task.Delay(Delay); times++; } var mockVm = new Mock <IViewModelBase>(); var dts = new DeterministicTaskScheduler(shouldThrowExceptions: false); ICommand command = new SafeCommand <string>(MockTask, dts, mockVm.Object); command.Execute("test"); command.Execute("test"); dts.RunTasksUntilIdle(); Assert.Equal(1, times); mockVm.VerifyGet(vm => vm.IsBusy, Times.Exactly(2)); mockVm.VerifySet(vm => vm.IsBusy = true); mockVm.VerifySet(vm => vm.IsBusy = false); }
public void ExecuteT_SecondCallAfterException_Executes() { int times = 0; async Task MockTask(string text) { await Task.Delay(Delay); if (times++ == 0) { throw new Exception(); //Throws only on first try } } var dts = new DeterministicTaskScheduler(shouldThrowExceptions: false); ICommand command = new SafeCommand <string>(MockTask, dts, null, null); command.Execute("test"); dts.RunTasksUntilIdle(); command.Execute("test"); dts.RunTasksUntilIdle(); Assert.NotEmpty(dts.Exceptions); Assert.Equal(2, times); }
public void Execute_SecondCallAfterException_Executes() { SafeExecutionHelpers.RevertToDefaultImplementation(); var mockHelpers = new Mock <ISafeExecutionHelpers>(); SafeExecutionHelpers.Implementation = mockHelpers.Object; Exception exception = new Exception(); int times = 0; void MockTask(string text) { if (times++ == 0) { throw exception; //Throws only on first try } } ICommand command = new SafeCommand <string>(MockTask); //Assert.Throws<Exception>(()=>command.Execute("test")); //First run command.Execute("test"); mockHelpers.Verify(h => h.HandleException <Exception>(exception, null)); //Second run command.Execute("test"); Assert.Equal(2, times); SafeExecutionHelpers.RevertToDefaultImplementation(); }
public void ExecuteAsyncT_RunsOnTaskPool(string parameter) { ICommand command = new SafeCommand <string>(MockTask); bool isExecuting = true; Thread callingThread = null, executingThread = null; async Task MockTask(string text) { await Task.Delay(Delay); executingThread = Thread.CurrentThread; isExecuting = false; } var thread = new Thread(new ThreadStart(() => { callingThread = Thread.CurrentThread; command.Execute(parameter); })); thread.Start(); while (isExecuting) { Thread.Sleep(Delay / 25); } Assert.False(callingThread.IsThreadPoolThread); Assert.True(executingThread.IsThreadPoolThread); }
public void ExecuteAsyncT_WithIBusy_IsBusyTrueWhileRunning(int number) { bool isExecuting = true; var command = new SafeCommand <int>(async(i) => { //True while running iBusyMock.VerifySet(o => o.IsBusy = true); await Task.Delay(DELAY); //Still True while running iBusyMock.VerifySet(o => o.IsBusy = true); iBusyMock.Invocations.Clear(); isExecuting = false; } , iBusyMock.Object); var thread = new Thread(new ThreadStart(() => command.Execute(number))); //False before start Assert.False(iBusyMock.Object.IsBusy); thread.Start(); while (isExecuting) { Thread.Sleep(DELAY / 25); } Thread.Sleep(5); //False after run iBusyMock.VerifySet(o => o.IsBusy = false); Assert.Equal(1, iBusyMock.Invocations.Count); }
public void ExecuteAsync_RunsOnNewThread() { ICommand command = new SafeCommand(MockTask); bool isExecuting = true; Thread callingThread = null, executingThread = null; async Task MockTask() { await Task.Delay(Delay); executingThread = Thread.CurrentThread; isExecuting = false; } var thread = new Thread(new ThreadStart(() => { callingThread = Thread.CurrentThread; command.Execute(null); })); thread.Start(); while (isExecuting) { Thread.Sleep(Delay / 25); } Assert.NotEqual(callingThread.ManagedThreadId, executingThread.ManagedThreadId); }
public void ExecuteAsync_MustRunOnCurrentSyncContextTrue_RunsOnCurrentThread() { ICommand command = new SafeCommand(MockTask, mustRunOnCurrentSyncContext: true); bool isExecuting = true; Thread callingThread = null, executingThread = null; async Task MockTask() { executingThread = Thread.CurrentThread; await Task.Delay(Delay).ConfigureAwait(true); isExecuting = false; } var thread = new Thread(new ThreadStart(() => { callingThread = Thread.CurrentThread; command.Execute(null); })); thread.Start(); while (isExecuting) { Thread.Sleep(Delay / 25); } Assert.Equal(callingThread.ManagedThreadId, executingThread.ManagedThreadId); }
public void ExecuteAsync_WithIBusy_IsBusyTrueWhileRunning() { bool isExecuting = true; var command = new SafeCommand(async() => { //State is true when start executing iBusyMock.VerifySet(o => o.IsBusy = true); await Task.Delay(DELAY); //State remains true throughout execution iBusyMock.VerifySet(o => o.IsBusy = true); iBusyMock.Invocations.Clear(); isExecuting = false; }, iBusyMock.Object); var thread = new Thread(new ThreadStart(() => command.Execute(null))); //Initial state is false Assert.False(iBusyMock.Object.IsBusy); //Act thread.Start(); while (isExecuting) { Thread.Sleep(DELAY / 25); } Thread.Sleep(5); //State is false after execution iBusyMock.VerifySet(o => o.IsBusy = false); //Only invoked once after set to true Assert.Equal(1, iBusyMock.Invocations.Count); }
public void ExecuteAsync_MustRunOnCurrentSyncContextTrue_NotRunOnTaskPool() { ICommand command = new SafeCommand(MockTask, mustRunOnCurrentSyncContext: true); bool isExecuting = true; Thread callingThread = null, executingThread = null; async Task MockTask() { executingThread = Thread.CurrentThread; await Task.Delay(Delay); isExecuting = false; } var thread = new Thread(new ThreadStart(() => { callingThread = Thread.CurrentThread; command.Execute(null); })); thread.Start(); while (isExecuting) { Thread.Sleep(Delay / 25); } Assert.False(executingThread.IsThreadPoolThread); Assert.False(callingThread.IsThreadPoolThread); }
public void Execute_ValueTypeAndSetToNull_ThrowsInvalidCommandParameterException() { int executions = 0; var Command = new SafeCommand <int>(executeAction: context => executions += 1); Assert.Throws <InvalidCommandParameterException>(() => Command.Execute(null)); Assert.True(executions == 0, "the Command should not have executed"); }
public void ExecuteWithCanExecute() { bool executed = false; var cmd = new SafeCommand(() => executed = true, canExecute: () => true); cmd.Execute(null); Assert.True(executed); }
public void Execute_ParameterIsWrongValueType_ThrowsInvalidCommandParameterException() { int executions = 0; var Command = new SafeCommand <int>(executeAction: context => executions += 1); Assert.Throws <InvalidCommandParameterException>(() => Command.Execute(10.5)); Assert.True(executions == 0); // "the Command should not have executed"); }
public void Execute_ParameterIsWrongReferenceType_ThrowsInvalidCommandParameterException() { int executions = 0; var Command = new SafeCommand <FakeChildContext>(executeAction: context => executions += 1); Assert.Throws <InvalidCommandParameterException>(() => Command.Execute(new FakeParentContext())); Assert.True(executions == 0); //, "the Command should not have executed"); }
public void GenericExecuteWithCanExecute() { string result = null; var cmd = new SafeCommand <string>(s => result = s, canExecute: s => true); cmd.Execute("Foo"); Assert.Equal("Foo", result); }
public void Execute_CalledTwice_SecondExecuteWaitsForFirstToComplete() { //Runs synchronously int times = 0; void MockTask(string text) { Thread.Sleep(10); times++; } ICommand command = new SafeCommand <string>(MockTask); command.Execute("test"); command.Execute("test"); Assert.Equal(2, times); }
public void Execute_CalledTwice_FiresTwiceIfNotBusy() { int times = 0; void MockTask(string text) { times++; } var dts = new DeterministicTaskScheduler(shouldThrowExceptions: false); ICommand command = new SafeCommand <string>(MockTask); command.Execute("test"); dts.RunTasksUntilIdle(); command.Execute("test"); dts.RunTasksUntilIdle(); Assert.Equal(2, times); }
public void ExecuteParameterized() { object executed = null; var cmd = new SafeCommand <object>(executeAction: o => executed = o); var expected = new object(); cmd.Execute(expected); Assert.Equal(expected, executed); }
public void Execute_NullableAndSetToNull_Runs() { int executions = 0; var Command = new SafeCommand <int?>(executeAction: context => executions += 1); var exception = Record.Exception(() => Command.Execute(null)); //"null is a valid value for a Nullable<int> type"); Assert.Null(exception); Assert.True(executions == 1); // "the Command should have executed"); }
public void ExecuteRunsIfReferenceTypeAndSetToNull() { int executions = 0; var Command = new SafeCommand <FakeChildContext>(context => executions += 1); var exception = Record.Exception(() => Command.Execute(null)); Assert.Null(exception); //"null is a valid value for a reference type"); Assert.True(executions == 1, "the Command should have executed"); }
public void ExecuteT_WithIViewModelBase_IsBusyTrueWhileRunning(int number) { var vm = new MockViewModel(); var command = new SafeCommand <int>(executeAction: (i) => { Assert.True(vm.IsBusy); }, vm); Assert.False(vm.IsBusy); command.Execute(number); //see Assert in command Assert.False(vm.IsBusy); }
public void Execute_WithIBusy_IsBusyTrueWhileRunning() { var vm = new MockViewModel(); var command = new SafeCommand(executeAction: () => { Assert.True(vm.IsBusy); }, vm); Assert.False(vm.IsBusy); command.Execute(null); //see Assert in command Assert.False(vm.IsBusy); }
public void ExecuteT_isBlockingFalse_CalledTwice_FiresTwiceIfBusy() { int times = 0; async Task MockTask(string text) { await Task.Delay(Delay); times++; } var dts = new DeterministicTaskScheduler(shouldThrowExceptions: false); ICommand command = new SafeCommand <string>(MockTask, dts, isBlocking: false); command.Execute("test"); command.Execute("test"); dts.RunTasksUntilIdle(); Assert.Equal(2, times); }
public void AsyncCommand_ExecuteAsync_StringParameter_Test(string parameter) { //Arrange var dts = new DeterministicTaskScheduler(); ICommand command = new SafeCommand <string>(StringParameterTask, dts, null, null); //Act command.Execute(parameter); dts.RunTasksUntilIdle(); //Assert }
public async Task ExecuteT_ViewModelIsBusy_NotRun(int number) { bool hasRun = false; iBusyMock.SetupGet(o => o.IsBusy).Returns(true); var command = new SafeCommand <int>(executeAction: (i) => { hasRun = true; }, iBusyMock.Object); await Task.Run(() => command.Execute(number)); Assert.False(hasRun); }
public async Task ExecuteT_ViewModelIsBusy_NotRun(int number) { bool hasRun = false; var vm = new MockViewModel { IsBusy = true }; var command = new SafeCommand <int>(executeAction: (i) => { hasRun = true; }, vm); await Task.Run(() => command.Execute(number)); Assert.False(hasRun); }
public void Execute_OnExceptionSet_SecondCallAfterException_Executes() { int times = 0; bool isHandled = false; void MockTask(string text) { Thread.Sleep(10); if (times++ == 0) { throw new Exception(); //Throws only on first try } } ICommand command = new SafeCommand <string>(MockTask, onException: (ex) => { isHandled = true; }); command.Execute("test"); //should throw command.Execute("test"); Assert.True(isHandled); Assert.Equal(2, times); }
public void ExecuteAsyncT_ViewModelIsBusy_NotRun(int number) { bool hasRun = false; iBusyMock.SetupGet(o => o.IsBusy).Returns(true); var command = new SafeCommand <int>(async(i) => { hasRun = true; await Task.Delay(DELAY); }, iBusyMock.Object); var thread = new Thread(new ThreadStart(() => command.Execute(number))); thread.Start(); Thread.Sleep(DELAY); Assert.False(hasRun); }
public void ExecuteAsyncT_ViewModelIsBusy_NotRun(int number) { bool hasRun = false; var vm = new MockViewModel { IsBusy = true }; var command = new SafeCommand <int>(async(i) => { hasRun = true; await Task.Delay(DELAY); }, vm); var thread = new Thread(new ThreadStart(() => command.Execute(number))); thread.Start(); Thread.Sleep(DELAY); Assert.False(hasRun); }
public void ExecuteT_WithIBusy_IsBusyTrueWhileRunning(int number) { var command = new SafeCommand <int>(executeAction: (i) => { //True while running iBusyMock.VerifySet(o => o.IsBusy = true); iBusyMock.Invocations.Clear(); }, iBusyMock.Object); //False before run Assert.False(iBusyMock.Object.IsBusy); //Act command.Execute(number); //False after run iBusyMock.VerifySet(o => o.IsBusy = false); Assert.Equal(1, iBusyMock.Invocations.Count); }
public void ExecuteAsyncT_WithIViewModelBase_IsBusyTrueWhileRunning(int number) { bool isExecuting = true; var vm = new MockViewModel(); var command = new SafeCommand <int>(async(i) => { Assert.True(vm.IsBusy); await Task.Delay(DELAY); isExecuting = false; }, vm); var thread = new Thread(new ThreadStart(() => command.Execute(number))); Assert.False(vm.IsBusy); thread.Start(); //see Assert in command while (isExecuting) { Thread.Sleep(DELAY / 25); } Thread.Sleep(5); Assert.False(vm.IsBusy); }