2015-10-01 68 views
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的一部分,但我找不到任何方法來獲得現有的可視化樹或創建一個新的。)我的問題是 - 我怎麼能做到這一點測試工作?

回答

0

我已經解決了這個問題。
使用asyncawait有助於避免掛起。

[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]); 
} 

好的。現在測試不掛,但我得到以下異常:

結果消息:試驗方法UnitTestApp.MyUnitTest.TestMethod2拋出 異常信息:System.NullReferenceException:未設置爲 一個對象的實例對象引用。

原因是element不是VisualTree的一部分。我找到了解決這個問題的方法:

var grid = Window.Current.Content as Grid; 
grid.Children.Add(element); 

不要忘了在最後刪除element。並且最終版本的測試:

[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]); 
}