public void TestAsyncRelayCommandNoParam() { const int processDuration = 500; AutoResetEvent ev = new AutoResetEvent(false); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); bool asyncActionPerformed = false; bool actionFinished = false; bool canExecutePerformed = false; ICommand command = new AsyncRelayCommand( // ReSharper disable ImplicitlyCapturedClosure () => { Thread.Sleep(processDuration); asyncActionPerformed = true; }, () => { Assert.That(asyncActionPerformed, Is.True, "Async method call should be called before finished action."); actionFinished = true; ev.Set(); }, () => { canExecutePerformed = true; return true; } // ReSharper restore ImplicitlyCapturedClosure ); Assert.That(command.CanExecute(null), Is.True); command.Execute(null); Assert.That(command.CanExecute(null), Is.False); Assert.That(stopWatch.ElapsedMilliseconds, Is.LessThan(processDuration)); bool exitBySignal = ev.WaitOne(2000); Assert.That(exitBySignal, Is.True, "Timeout by waiting for command completion."); Assert.That(asyncActionPerformed, Is.True); Assert.That(actionFinished, Is.True); Assert.That(canExecutePerformed, Is.True); }
public void TestAsyncRelayCommandWithParamNoFinishedaction() { const int processDuration = 500; const string parameterString = "123"; int parameterAsInt = int.Parse(parameterString); AutoResetEvent ev = new AutoResetEvent(false); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); bool asyncActionPerformed = false; bool actionFinished = false; bool canExecutePerformed = false; string executeActionParam = string.Empty; string executeFinishedParam = string.Empty; AsyncRelayCommand<string> command = new AsyncRelayCommand<string>( // ReSharper disable ImplicitlyCapturedClosure e => { executeActionParam = e; Thread.Sleep(processDuration); asyncActionPerformed = true; executeFinishedParam = e; actionFinished = true; ev.Set(); }, e => { canExecutePerformed = true; Assert.That(e, Is.EqualTo(parameterString)); return true; } // ReSharper restore ImplicitlyCapturedClosure ); Assert.That(command.CanExecute(parameterString), Is.True); Assert.That(command.CanExecute(parameterAsInt), Is.True); command.Execute(parameterString); Assert.That(command.CanExecute(parameterString), Is.False); Assert.That(stopWatch.ElapsedMilliseconds, Is.LessThan(processDuration)); bool exitBySignal = ev.WaitOne(2000); Assert.That(exitBySignal, Is.True, "Timeout by waiting for command completion."); // Check convertible parameter. Assert.That(asyncActionPerformed, Is.True); Assert.That(actionFinished, Is.True); Assert.That(canExecutePerformed, Is.True); Assert.That(executeActionParam, Is.EqualTo(parameterString)); Assert.That(executeFinishedParam, Is.EqualTo(parameterString)); }