The Issue
Ideally, in testing, an elegant system might be setup in such a way that a tester could do the following,
Mouse.LeftMouseButton = Mouse.Pressed;
Assert.SomethingHappened(() => { sut.MoveMouseTo(x, y) });
As it turns out it's not quite as simple as that in the real world. After much research, it appears that one may need to go through, third party libraries, or inherit from xyz to finally be able to say to the application, MOUSEDOWN... I refuse to believe that the current state of things is as limited as that.
The Current State
Let's take a look at what I've currently written to show just how fundamentally wrong I misunderstand event raising in general.
[Test]
[Apartment(ApartmentState.STA)]
public void CallToDragWindowMouseDown_DoesNotThrowException()
{
//Arrange
Dispatcher.CurrentDispatcher.InokeShutdown();
IMainWindowViewModelFactory vm = A.Fake<IMainWindowViewModelFactory>();
Window window = new();
MainWindowAdapter adapter = new(window, vm);
//Act
adapter.show();
MouseButtonEventArgs args = new(InputerManager.Current.PrimaryMouseDevice, 0, MouseButton.Left)
{
RoutedEvent = Mouse.MouseDownEvent,
Source = InputManager.Current
};
InputManager.Current.ProcessInput(args);
//Assert
Assert.DoesNotThrow(() =>
{
adapter.DragWindow();
});
}
Now let's take a moment to appreciate that this test is nearly useless but for the sake of science it must produce the desired results so I might sleep easier.
The error that occurs is that the InputManger.Current.ProcessInput(args) results in false, implying that the arguments could not be processed as inputs. Furthermore, the InputManger.Current.PrimaryMouseDevice.LeftMouseDown remains as Mouse.Released rather than Mouse.Pressed. Resulting in an InvalidOperationException that reads to the effect "Cannot Call DragMove() if the Primary Mouse Button is not Pressed.".
I assume now because I am not directly starting the application that it's possible the InputManager doesn't have all that it needs to do what I desire. I've noticed that the InputManager.Current.PrimaryMouseDevice does not contain a PresentationSource or various other properties that sound vaguely important.
The Solution?
Issues may arise due to the application under test not being fully instantiated despite my best attempts at loose coupling to promote this kind of test-ability.
Issues may have arisen due to my misunderstanding of the InputManager and what exactly needs to be done to invoke an operation.
Other than that, It's not like I need to test what is essentially a window wrapper, but this is the curse I live with, I'm curious.
Thanks,