0
public class UnitTest1
{
    // you cant use a brush because it is UI
    // you switch to a UI test and it fails because it is async
    // bottom line you cant test an async routine that uses a brush,
    // even though your really are not doing ANY UI stuff - how stupid is that?

    [TestMethod]
    //[Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AppContainer.UITestMethod]
    async public Task TestMethod1()
    {
        var t = new ObservableCollection<CommentEvent>();
        t.Add(new CommentEvent(Colors.LightPink) { Label = "Bad", 
              EventTime = TimeCode.FromTicks(DateTime.Now.Ticks,
              TimeCode.SmpteFrameRate.Smpte2997Drop) });
        t.Add(new CommentEvent(Colors.DarkSeaGreen) { Label = "Good",
               EventTime = TimeCode.FromTicks(DateTime.Now.Ticks,
               TimeCode.SmpteFrameRate.Smpte2997Drop) });
        t.Add(new CommentEvent(Colors.LightPink) { Label = "Bad",
               EventTime = TimeCode.FromTicks(DateTime.Now.Ticks,
               TimeCode.SmpteFrameRate.Smpte2997Drop) });
        t.Add(new CommentEvent(Colors.DarkSeaGreen) { Label = "Good",
               EventTime = TimeCode.FromTicks(DateTime.Now.Ticks,
               TimeCode.SmpteFrameRate.Smpte2997Drop) });

        var s = await PreludeXMP.Get(t);

        Assert.IsNotNull(s);
        System.Diagnostics.Debug.WriteLine(s);
    }
}

this throws a

The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)). If you are using UI objects in test consider using [UITestMethod] attribute instead of [TestMethod] to execute test in UI thread.

Because the constructor for CommentEvent creates a Solid.Brush. Any suggested workaroundsarounds?

Noctis
  • 11,507
  • 3
  • 43
  • 82
Spiked3
  • 337
  • 6
  • 18
  • 2
    The accepted answer to the following question might help: [Unit Testing Windows 8 Store App UI (Xaml Controls)](http://stackoverflow.com/questions/14118956/unit-testing-windows-8-store-app-ui-xaml-controls) – chue x Nov 17 '13 at 02:25
  • Well, if that answers your question (did read it), you can post it as solution :) – Noctis Nov 17 '13 at 02:48
  • Curious; the exception suggests " If you are using UI objects in test consider using [UITestMethod]" and I have both "// you switch to a UI test and it fails because it is async" and "//[Microsoft.VisualStudio.TestPlatform.UnitTestFramework.AppContainer.UITestMethod]" in comments. How did you interpret that, how could I be clearer? – Spiked3 Nov 17 '13 at 02:50

1 Answers1

0

I encountered a very similar issue when trying to use WriteableBitmap along with an asynchronous method in one of my tests. While searhing for a way to make it work, I stumbled upon your question and chue x's answer mentioned the comments.

Using his approach I managed to make it work even with asynchronous calls. In your case the test code could be modified like this:

[TestMethod]
async public Task TestMethod1()
{
    var taskSource = new TaskCompletionSource<object>();
    await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(
        CoreDispatcherPriority.Normal, async () =>
    {
        try
        {
            var t = new ObservableCollection<CommentEvent>();
            t.Add(new CommentEvent(Colors.LightPink) { Label = "Bad", 
                  EventTime = TimeCode.FromTicks(DateTime.Now.Ticks,
                  TimeCode.SmpteFrameRate.Smpte2997Drop) });
            t.Add(new CommentEvent(Colors.DarkSeaGreen) { Label = "Good",
                   EventTime = TimeCode.FromTicks(DateTime.Now.Ticks,
                   TimeCode.SmpteFrameRate.Smpte2997Drop) });
            t.Add(new CommentEvent(Colors.LightPink) { Label = "Bad",
                   EventTime = TimeCode.FromTicks(DateTime.Now.Ticks,
                   TimeCode.SmpteFrameRate.Smpte2997Drop) });
            t.Add(new CommentEvent(Colors.DarkSeaGreen) { Label = "Good",
                   EventTime = TimeCode.FromTicks(DateTime.Now.Ticks,
                   TimeCode.SmpteFrameRate.Smpte2997Drop) });

            var s = await PreludeXMP.Get(t);

            Assert.IsNotNull(s);
            System.Diagnostics.Debug.WriteLine(s);

            askSource.SetResult(null);
        }
        catch (Exception e)
        {
            taskSource.SetException(e);
        }
    }
    await taskSource.Task;
}

I've written a more in depth explanation with my own example in a blog post that I've just published.

Community
  • 1
  • 1
Damir Arh
  • 17,637
  • 2
  • 45
  • 83
  • Thanks. I think I gave up and just dropped the UI tests. heh, to be honest, its been so long I forget. I know the app was 'finished' to the point it is not being worked on anymore, so I probably will not get a chance to try it right away. To me it is just another detail, that got missed. Maybe next release. – Spiked3 Dec 09 '13 at 06:26