Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing WPF Control Without Adding it to a Window

I have a UserControl which publishes an EventAggregator message in its Loaded event. In order to test this (and get the Loaded event raised) I am currently creating a window and adding the control to it, then waiting for the Loaded event to be raised.

Is there any way to setup a test so that the Loaded event fires without having to create and add the control to a window?

For example:

[Test, RequiresSTA]
public void active_thingy_message_is_published_on_loaded()
{
    const string TestMsg = "Active thingy changed";

    using (AutoResetEvent loadedEvent = new AutoResetEvent(false))
    {
        DummyEventService eventService = new DummyEventService();                
        DummyControl control = new DummyControl(eventService, TestMsg);
        control.Loaded += delegate { loadedEvent.Set(); };

        Assert.That(eventService.Message, Is.Null, "Before.");
        Window window = new Window { Content = control };
        window.Show();                
        loadedEvent.WaitOne();
        window.Dispatcher.InvokeShutdown();
        Assert.That(eventService.Message, Is.EqualTo(TestMsg), "After.");
    }
}

private class DummyControl : UserControl
{
    public DummyControl(DummyEventService eventService, string testMsg)
    {
        Loaded += delegate { eventService.Publish(testMsg); };
    }
}

private class DummyEventService
{
    public string Message { get; private set; }
    public void Publish(string msg) { Message = msg; }
}

Update

I've changed the title from "Unit Testing..." to "Testing...", and replaced the tag "unit-testing" with "testing".

I would prefer not to split hairs over exactly what class of test this is, as it is not constructive. Yes it could be argued that this is not a "Unit Test", but that's not helpful. I want to test an issue that is dependent on the control's life-cycle and this involves the Loaded event. It's an important regression test, as 3rd party components I have no control over depend on the message being raised at Loaded.

Can the Loaded event be raised without adding the control to a window?

like image 542
Tim Lloyd Avatar asked Aug 19 '11 22:08

Tim Lloyd


2 Answers

If you are just interested in firing the Loaded event of the target control, then Reflection should do the trick.

public static void RaiseLoadedEvent(FrameworkElement element)
{
    MethodInfo eventMethod = typeof(FrameworkElement).GetMethod("OnLoaded",
        BindingFlags.Instance | BindingFlags.NonPublic);

    RoutedEventArgs args = new RoutedEventArgs(FrameworkElement.LoadedEvent);

    eventMethod.Invoke(element, new object[] { args });
}

This literally fires the OnLoaded method that is present in each FrameworkElement, so if your test requires Application state, this won't work.

Also, there is no relationship between the Loaded event of a parent and it's children. If a test requires the child elements to fire their Loaded events, then the helper method will need to manually walk the child controls and fire those as well.

like image 174
Paul Walls Avatar answered Nov 10 '22 04:11

Paul Walls


In the past two years, perhaps things have changed. For the sake of code coverage, I ran into this problem as well, and it's solution.

WPF UIElements inherit a method called RaiseEvent, which takes a RoutedEventArgs. This can be constructed using the particular UIElement's .LoadedEvent, allowing you to get that final bit of code coverage.

I doubt you still need my answer, but someone may.

like image 8
Magus Avatar answered Nov 10 '22 04:11

Magus