1
我想寫一個單元測試,將測試我的自定義控件的視覺外觀。因此,我想使用RenderTargetBitmap來捕捉控制視覺外觀,然後在結果像素上使用斷言。下面是我的測試:RenderTargetBitmap在Windows Phone單元測試應用程序
[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的一部分,但我找不到任何方法來獲得現有的可視化樹或創建一個新的。)我的問題是 - 我怎麼能做到這一點測試工作?