I am trying to unit test a WPF control and need to simulate key down presses. I have seen a possible solution here, however when I try to pass in a PresentationSource I keep getting a null value (from either PresentationSource.FromVisual() or PresentationSource.FromDependencyObject()) which triggers an exception.
My question is how do I get a non-null PresentationSource that I can use in unit tests?
You can extend the PresentationSource class like this:
public class FakePresentationSource : PresentationSource
{
protected override CompositionTarget GetCompositionTargetCore()
{
return null;
}
public override Visual RootVisual { get; set; }
public override bool IsDisposed { get { return false; } }
}
And use it like this:
var uiElement = new UIElement();
uiElement.RaiseEvent(new KeyEventArgs(Keyboard.PrimaryDevice, new FakePresentationSource(), 0, Key.Delete)
{
RoutedEvent = UIElement.KeyDownEvent
});
A quicker solution for unit tests is just to mock the PresentationSource object. Note that it requires an STA thread. Sample uses Moq and nunit.
[Test]
[RequiresSTA]
public void test_something()
{
new KeyEventArgs(
Keyboard.PrimaryDevice,
new Mock<PresentationSource>().Object,
0,
Key.Back);
}
Figured this out after reading this post.
Basically, you need to put your control inside a Window and call Window.Show() on it. The post mentioned an WPF bug, but I didn't encounter this in WPF 4.
After calling Window.Show(), the presentation source will no longer be null and you will be able to send keys to the control.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With