Windows Phone 单元测试应用程序中的 RenderTargetBitmap [英] RenderTargetBitmap in Windows Phone Unit Test App
问题描述
我正在尝试编写一个单元测试来测试我的自定义控件的视觉外观.因此,我想使用 RenderTargetBitmap 捕获控件视觉外观,然后对结果像素使用断言.这是我的测试:
I'm trying to write a unit test that will test a visual appearance of my custom control. Therefore I want to capture the control visual appearance using RenderTargetBitmap and then use assertions on result pixels. Here is my test:
[TestClass]
public class UnitTest2
{
[TestMethod]
public void TestMethod2()
{
//pixels to test
byte[] pixels = null;
// Since I create a UI elements, I need to use Dispatcher here.
var task = CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, () =>
{
// Here I create a visual element that I want to test
var brush = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
var element = new Border()
{
MinWidth = 20,
MinHeight = 20,
Background = brush,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
BorderThickness = new Thickness(0)
};
// create an instance of RenderTargetBitmap to render a bitmap
var rtb = new RenderTargetBitmap();
var rtbTask = rtb.RenderAsync(element).AsTask();
// the following operation lasts for ages
// May be the reason is that the element is not a part of the VisualTree
rtbTask.Wait();
var rtbGetPixelsTask = rtb.GetPixelsAsync().AsTask<IBuffer>();
rtbGetPixelsTask.Wait();
pixels = rtbGetPixelsTask.Result.ToArray();
});
task.AsTask().Wait();
Assert.AreEqual<byte>(255, pixels[0]);
Assert.AreEqual<byte>(255, pixels[1]);
Assert.AreEqual<byte>(255, pixels[2]);
Assert.AreEqual<byte>(255, pixels[3]);
}
}
问题是,当我运行此测试时,RendreAsync()
操作将永远持续下去.
(其原因可能是 element
不是 VisualTree 的一部分,但我找不到任何方法来获取现有的可视化树或创建一个新的可视化树.)
我的问题是 - 我怎样才能使这个测试工作?
The problem is that when I run this test the RendreAsync()
operation lasts forever.
(The cause of that could be that the element
is not a part of the VisualTree, but I cannot find any way to get existing visual tree or to create a new one.)
My question is - how can I make this test work?
推荐答案
我已经解决了这个问题.
使用 async
await
有助于避免挂起.
I've solved the issue.
Using async
await
helps to avoid hanging.
[TestMethod]
public async Task TestMethod2()
{
//pixels to test
byte[] pixels = null;
// Since I create a UI elements, I need to use Dispatcher here.
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, async () =>
{
// Here I create a visual element that I want to test
var brush = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
var element = new Border()
{
MinWidth = 20,
MinHeight = 20,
Background = brush,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
BorderThickness = new Thickness(0)
};
// create an instance of RenderTargetBitmap to render a bitmap
var rtb = new RenderTargetBitmap();
await rtb.RenderAsync(element); // exception!
var pixBuffer = await rtb.GetPixelsAsync();
pixels = pixBuffer.ToArray();
});
Assert.AreEqual<byte>(255, pixels[0]);
Assert.AreEqual<byte>(255, pixels[1]);
Assert.AreEqual<byte>(255, pixels[2]);
Assert.AreEqual<byte>(255, pixels[3]);
}
好的.现在测试没有挂起,但我收到以下异常:
Ok. Now the test doesn't hang but I get the following exception:
结果消息:测试方法 UnitTestApp.MyUnitTest.TestMethod2 抛出异常:System.NullReferenceException:对象引用未设置为一个对象的实例.
Result Message: Test method UnitTestApp.MyUnitTest.TestMethod2 threw exception: System.NullReferenceException: Object reference not set to an instance of an object.
原因是 element
不是 VisualTree 的一部分.我找到了解决这个问题的方法:
The reason is that the element
is not a part of the VisualTree. I've found the way to fix that:
var grid = Window.Current.Content as Grid;
grid.Children.Add(element);
最后不要忘记删除element
.以及测试的最终版本:
Do not forget to remove the element
in the end. And the final version of the test:
[TestMethod]
public async Task TestMethod4()
{
//pixels to test
byte[] pixels = null;
// Since I create a UI elements, I need to use Dispatcher here.
await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.High, async () =>
{
var grid = Window.Current.Content as Grid;
// Here I create a visual element that I want to test
var brush = new SolidColorBrush(Color.FromArgb(255, 255, 0, 0));
var element = new Border()
{
MinWidth = 20,
MinHeight = 20,
Background = brush,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
BorderThickness = new Thickness(0)
};
grid.Children.Add(element);
try
{
// create an instance of RenderTargetBitmap to render a bitmap
var rtb = new RenderTargetBitmap();
await rtb.RenderAsync(element);
var pixBuffer = await rtb.GetPixelsAsync();
pixels = pixBuffer.ToArray();
}
finally
{
grid.Children.Remove(element);
}
});
Assert.AreEqual<byte>(255, pixels[0]);
Assert.AreEqual<byte>(255, pixels[1]);
Assert.AreEqual<byte>(255, pixels[2]);
Assert.AreEqual<byte>(255, pixels[3]);
}
这篇关于Windows Phone 单元测试应用程序中的 RenderTargetBitmap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!