Testing Windows 8 Store App UI Devices (Xaml Controls)

I am creating an application for the Windows Store, but I have problems with the thread that are testing a method that creates a Grid (which is a XAML control). I tried to test using NUnit and MSTest.

Testing method:

[TestMethod] public void CreateThumbnail_EmptyLayout_ReturnsEmptyGrid() { Layout l = new Layout(); ThumbnailCreator creator = new ThumbnailCreator(); Grid grid = creator.CreateThumbnail(l, 192, 120); int count = grid.Children.Count; Assert.AreEqual(count, 0); } 

And the creator of .CreateThumbnail (the method that causes the error):

 public Grid CreateThumbnail(Layout l, double totalWidth, double totalHeight) { Grid newGrid = new Grid(); newGrid.Width = totalWidth; newGrid.Height = totalHeight; SolidColorBrush backGroundBrush = new SolidColorBrush(BackgroundColor); newGrid.Background = backGroundBrush; newGrid.Tag = l; return newGrid; } 

When I run this test, it throws this error:

 System.Exception: The application called an interface that was marshalled for a different thread. (Exception from HRESULT: 0x8001010E (RPC_E_WRONG_THREAD)) 
+7
source share
1 answer

The appropriate control code must be run in the user interface thread. Try:

 [TestMethod] async public Task CreateThumbnail_EmptyLayout_ReturnsEmptyGrid() { int count = 0; await ExecuteOnUIThread(() => { Layout l = new Layout(); ThumbnailCreator creator = new ThumbnailCreator(); Grid grid = creator.CreateThumbnail(l, 192, 120); count = grid.Children.Count; }); Assert.AreEqual(count, 0); } public static IAsyncAction ExecuteOnUIThread(Windows.UI.Core.DispatchedHandler action) { return Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, action); } 

The above should work with MS Test. I do not know about NUnit.

+9
source

All Articles