129 lines
5.5 KiB
C#
129 lines
5.5 KiB
C#
using Moq;
|
|
using System.Windows.Controls;
|
|
using System.Windows.Threading;
|
|
using WpfViewModelFirst.Part1;
|
|
using WpfViewModelFirst.Part2;
|
|
|
|
namespace WpfViewModelFirst.Tests
|
|
{
|
|
public class MainWindowViewModelTests
|
|
{
|
|
[Fact]
|
|
public void Part1WillSetPart1ViewModelToCurrentViewModel()
|
|
{
|
|
Part1ViewModel part1ViewModel = new(() => { return Task.CompletedTask; });
|
|
Mock<IViewModelFactory> mockViewModelFactory = new();
|
|
mockViewModelFactory.Setup(vmf => vmf.GetPart1ViewModel(It.IsAny<Func<Task>>()))
|
|
.Returns(part1ViewModel);
|
|
MainWindowViewModel main = new(mockViewModelFactory.Object);
|
|
main.Part1(null);
|
|
Assert.Equal(part1ViewModel, main.CurrentViewModel);
|
|
}
|
|
//[Fact]
|
|
//public async Task Part1CallbackIsSuccessfull()
|
|
//{
|
|
// TaskCompletionSource<bool> taskCompletionSource = new();
|
|
// Mock<IViewModelFactory> mockViewModelFactory = new();
|
|
// mockViewModelFactory.Setup(vmf => vmf.GetPart1ViewModel(It.IsAny<Func<Task>>()))
|
|
// .Callback<Func<Task>>(c =>
|
|
// {
|
|
// Task.Run(() =>
|
|
// {
|
|
// c.Invoke();
|
|
// });
|
|
// });
|
|
// mockViewModelFactory.Setup(vmf => vmf.GetPart2ViewModel())
|
|
// .Returns(() => new Part2ViewModel());
|
|
// MainWindowViewModel main = new(mockViewModelFactory.Object);
|
|
// main.ItHappened += () => taskCompletionSource.SetResult(true);
|
|
// main.Part1(null);
|
|
// await taskCompletionSource.Task.WaitAsync(CancellationToken.None);
|
|
// Assert.IsType<Part2ViewModel>(main.CurrentViewModel);
|
|
// Assert.Single(main.Strings);
|
|
//}
|
|
[Fact]
|
|
public async Task Part1CallBackExample()
|
|
{
|
|
Mock<IViewModelFactory> mockViewModelFactory = new();
|
|
mockViewModelFactory.Setup(vmf => vmf.GetPart2ViewModel())
|
|
.Returns(() => new Part2ViewModel());
|
|
MainWindowViewModel main = new(mockViewModelFactory.Object);
|
|
TaskCompletionSource<bool> taskCompletionSource = new();
|
|
main.ItHappened += () => taskCompletionSource.SetResult(true);
|
|
mockViewModelFactory.Setup(vmf => vmf.GetPart1ViewModel(It.IsAny<Func<Task>>()))
|
|
.Callback<Func<Task>>(c =>
|
|
{
|
|
Task.Run(() =>
|
|
{
|
|
DispatcherFrame frame = new();
|
|
frame.Dispatcher.Invoke(
|
|
DispatcherPriority.Normal, () =>
|
|
{
|
|
c.Invoke();
|
|
frame.Continue = true;
|
|
});
|
|
Dispatcher.PushFrame(frame);
|
|
});
|
|
});
|
|
main.Part1(null);
|
|
await taskCompletionSource.Task.WaitAsync(CancellationToken.None);
|
|
Assert.IsType<Part2ViewModel>(main.CurrentViewModel);
|
|
Assert.Single(main.Strings);
|
|
}
|
|
// This method requires the Part1Action method to be internal not private
|
|
// The MainWindowViewModel class also needs to have an attribute allowing
|
|
// this assembly to see the internals
|
|
// [assembly:System.Runtime.CompilerServices.InternalsVisibleTo("WpfViewModelFirst.Tests")]
|
|
[Fact]
|
|
public async Task Part1ActionWillSetCurrentViewModel()
|
|
{
|
|
Mock<IViewModelFactory> mockViewModelFactory = new();
|
|
mockViewModelFactory.Setup(vmf => vmf.GetPart2ViewModel())
|
|
.Returns(() => new Part2ViewModel());
|
|
MainWindowViewModel main = new(mockViewModelFactory.Object);
|
|
TaskCompletionSource<bool> taskCompletionSource = new();
|
|
main.ItHappened += () => taskCompletionSource.SetResult(true);
|
|
|
|
Task run = new(() =>
|
|
{
|
|
DispatcherFrame frame = new();
|
|
frame.Dispatcher.Invoke(
|
|
DispatcherPriority.Normal, () =>
|
|
{
|
|
main.Part1Action();
|
|
frame.Continue = true;
|
|
});
|
|
Dispatcher.PushFrame(frame);
|
|
});
|
|
run.Start();
|
|
await taskCompletionSource.Task.WaitAsync(CancellationToken.None);
|
|
Assert.IsType<Part2ViewModel>(main.CurrentViewModel);
|
|
}
|
|
[Fact]
|
|
public async Task Part1ActionWillSetSingle()
|
|
{
|
|
Mock<IViewModelFactory> mockViewModelFactory = new();
|
|
mockViewModelFactory.Setup(vmf => vmf.GetPart2ViewModel())
|
|
.Returns(() => new Part2ViewModel());
|
|
MainWindowViewModel main = new(mockViewModelFactory.Object);
|
|
TaskCompletionSource<bool> taskCompletionSource = new();
|
|
main.ItHappened += () => taskCompletionSource.SetResult(true);
|
|
|
|
Task run = new(() =>
|
|
{
|
|
DispatcherFrame frame = new();
|
|
frame.Dispatcher.Invoke(
|
|
DispatcherPriority.Normal, () =>
|
|
{
|
|
main.Part1Action();
|
|
frame.Continue = true;
|
|
});
|
|
Dispatcher.PushFrame(frame);
|
|
});
|
|
run.Start();
|
|
await taskCompletionSource.Task.WaitAsync(CancellationToken.None);
|
|
Assert.Single(main.Strings);
|
|
}
|
|
|
|
}
|
|
} |