2
0

AvaloniaTestRunner.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Reflection;
  2. using Avalonia.Headless;
  3. using Avalonia.Platform;
  4. using Avalonia.Threading;
  5. using Drawie.Backend.Core.Bridge;
  6. using Drawie.Skia;
  7. using DrawiEngine;
  8. using PixiEditor.Desktop;
  9. using Xunit.Abstractions;
  10. using Xunit.Sdk;
  11. [assembly:TestFramework("PixiEditor.Tests.AvaloniaTestRunner", "PixiEditor.Tests")]
  12. [assembly:CollectionBehavior(CollectionBehavior.CollectionPerAssembly, DisableTestParallelization = false, MaxParallelThreads = 1)]
  13. namespace PixiEditor.Tests
  14. {
  15. public class AvaloniaTestRunner : XunitTestFramework
  16. {
  17. public AvaloniaTestRunner(IMessageSink messageSink) : base(messageSink)
  18. {
  19. }
  20. protected override ITestFrameworkExecutor CreateExecutor(AssemblyName assemblyName)
  21. => new Executor(assemblyName, SourceInformationProvider, DiagnosticMessageSink);
  22. class Executor : XunitTestFrameworkExecutor
  23. {
  24. public Executor(AssemblyName assemblyName, ISourceInformationProvider sourceInformationProvider,
  25. IMessageSink diagnosticMessageSink) : base(assemblyName, sourceInformationProvider,
  26. diagnosticMessageSink)
  27. {
  28. }
  29. protected override async void RunTestCases(IEnumerable<IXunitTestCase> testCases,
  30. IMessageSink executionMessageSink,
  31. ITestFrameworkExecutionOptions executionOptions)
  32. {
  33. executionOptions.SetValue("xunit.execution.DisableParallelization", false);
  34. using (var assemblyRunner = new Runner(
  35. TestAssembly, testCases, DiagnosticMessageSink, executionMessageSink,
  36. executionOptions)) await assemblyRunner.RunAsync();
  37. }
  38. }
  39. class Runner : XunitTestAssemblyRunner
  40. {
  41. public Runner(ITestAssembly testAssembly, IEnumerable<IXunitTestCase> testCases,
  42. IMessageSink diagnosticMessageSink, IMessageSink executionMessageSink,
  43. ITestFrameworkExecutionOptions executionOptions) : base(testAssembly, testCases, diagnosticMessageSink,
  44. executionMessageSink, executionOptions)
  45. {
  46. }
  47. protected override void SetupSyncContext(int maxParallelThreads)
  48. {
  49. var tcs = new TaskCompletionSource<SynchronizationContext>();
  50. new Thread(() =>
  51. {
  52. try
  53. {
  54. Program.BuildAvaloniaApp()
  55. .UseHeadless(new AvaloniaHeadlessPlatformOptions { FrameBufferFormat = PixelFormat.Bgra8888, UseHeadlessDrawing = false })
  56. .SetupWithoutStarting();
  57. tcs.SetResult(SynchronizationContext.Current);
  58. }
  59. catch (Exception e)
  60. {
  61. tcs.SetException(e);
  62. }
  63. Dispatcher.UIThread.MainLoop(CancellationToken.None);
  64. })
  65. {
  66. IsBackground = true
  67. }.Start();
  68. SynchronizationContext.SetSynchronizationContext(tcs.Task.Result);
  69. }
  70. }
  71. }
  72. }