TestBase.cs 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // -----------------------------------------------------------------------
  2. // <copyright file="TestBase.cs" company="Steven Kirk">
  3. // Copyright 2014 MIT Licence. See licence.md for more information.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. namespace Perspex.Direct2D1.RenderTests
  7. {
  8. using System.IO;
  9. using System.Runtime.CompilerServices;
  10. using ImageMagick;
  11. using Perspex.Controls;
  12. using Perspex.Media.Imaging;
  13. using Xunit;
  14. #if PERSPEX_CAIRO
  15. using Perspex.Cairo;
  16. #else
  17. using Perspex.Direct2D1;
  18. #endif
  19. public class TestBase
  20. {
  21. static TestBase()
  22. {
  23. #if PERSPEX_CAIRO
  24. CairoPlatform.Initialize();
  25. #else
  26. Direct2D1Platform.Initialize();
  27. #endif
  28. }
  29. public TestBase(string outputPath)
  30. {
  31. #if PERSPEX_CAIRO
  32. string testFiles = Path.GetFullPath(@"..\..\..\TestFiles\Cairo");
  33. #else
  34. string testFiles = Path.GetFullPath(@"..\..\..\TestFiles\Direct2D1");
  35. #endif
  36. this.OutputPath = Path.Combine(testFiles, outputPath);
  37. }
  38. public string OutputPath
  39. {
  40. get;
  41. private set;
  42. }
  43. protected void RenderToFile(Control target, [CallerMemberName] string testName = "")
  44. {
  45. string path = Path.Combine(this.OutputPath, testName + ".out.png");
  46. RenderTargetBitmap bitmap = new RenderTargetBitmap(
  47. (int)target.Width,
  48. (int)target.Height);
  49. Size size = new Size(target.Width, target.Height);
  50. target.Measure(size);
  51. target.Arrange(new Rect(size));
  52. bitmap.Render(target);
  53. bitmap.Save(path);
  54. }
  55. protected void CompareImages([CallerMemberName] string testName = "")
  56. {
  57. string expectedPath = Path.Combine(this.OutputPath, testName + ".expected.png");
  58. string actualPath = Path.Combine(this.OutputPath, testName + ".out.png");
  59. MagickImage expected = new MagickImage(expectedPath);
  60. MagickImage actual = new MagickImage(actualPath);
  61. double error = expected.Compare(actual, ErrorMetric.RootMeanSquared);
  62. if (error > 0.02)
  63. {
  64. Assert.True(false, actualPath + ": Error = " + error);
  65. }
  66. }
  67. }
  68. }