TestBase.cs 5.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Copyright (c) The Perspex Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System.IO;
  4. using System.Runtime.CompilerServices;
  5. using ImageMagick;
  6. using Perspex.Controls;
  7. using Perspex.Media.Imaging;
  8. using Perspex.Rendering;
  9. using Xunit;
  10. #if PERSPEX_CAIRO
  11. using Perspex.Cairo;
  12. #elif PERSPEX_SKIA
  13. using Perspex.Skia;
  14. #else
  15. using Perspex.Direct2D1;
  16. #endif
  17. #if PERSPEX_CAIRO
  18. namespace Perspex.Cairo.RenderTests
  19. #elif PERSPEX_SKIA
  20. namespace Perspex.Skia.RenderTests
  21. #else
  22. namespace Perspex.Direct2D1.RenderTests
  23. #endif
  24. {
  25. public class TestBase
  26. {
  27. static TestBase()
  28. {
  29. #if PERSPEX_CAIRO
  30. CairoPlatform.Initialize();
  31. #elif PERSPEX_SKIA
  32. SkiaPlatform.Initialize();
  33. #else
  34. Direct2D1Platform.Initialize();
  35. #endif
  36. }
  37. public TestBase(string outputPath)
  38. {
  39. #if PERSPEX_CAIRO
  40. string testFiles = Path.GetFullPath(@"..\..\..\TestFiles\Cairo");
  41. #elif PERSPEX_SKIA
  42. string testFiles = Path.GetFullPath(@"..\..\..\TestFiles\Skia");
  43. #else
  44. string testFiles = Path.GetFullPath(@"..\..\..\TestFiles\Direct2D1");
  45. #endif
  46. OutputPath = Path.Combine(testFiles, outputPath);
  47. }
  48. public string OutputPath
  49. {
  50. get;
  51. }
  52. protected void RenderToFile(Control target, [CallerMemberName] string testName = "")
  53. {
  54. string path = Path.Combine(OutputPath, testName + ".out.png");
  55. using (RenderTargetBitmap bitmap = new RenderTargetBitmap(
  56. (int)target.Width,
  57. (int)target.Height))
  58. {
  59. Size size = new Size(target.Width, target.Height);
  60. target.Measure(size);
  61. target.Arrange(new Rect(size));
  62. bitmap.Render(target);
  63. bitmap.Save(path);
  64. }
  65. }
  66. protected void CompareImages([CallerMemberName] string testName = "")
  67. {
  68. string expectedPath = Path.Combine(OutputPath, testName + ".expected.png");
  69. string actualPath = Path.Combine(OutputPath, testName + ".out.png");
  70. MagickImage expected = new MagickImage(expectedPath);
  71. MagickImage actual = new MagickImage(actualPath);
  72. double error = expected.Compare(actual, ErrorMetric.RootMeanSquared);
  73. if (error > 0.022)
  74. {
  75. Assert.True(false, actualPath + ": Error = " + error);
  76. }
  77. }
  78. }
  79. }