PixiEditorTest.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using Drawie.Backend.Core;
  2. using Drawie.Backend.Core.Bridge;
  3. using Drawie.Interop.Avalonia.Core;
  4. using Drawie.Numerics;
  5. using Drawie.RenderApi;
  6. using Drawie.RenderApi.OpenGL;
  7. using Drawie.RenderApi.Vulkan;
  8. using Drawie.Silk;
  9. using Drawie.Skia;
  10. using Drawie.Windowing;
  11. using DrawiEngine;
  12. using DrawiEngine.Desktop;
  13. using Microsoft.Extensions.DependencyInjection;
  14. using PixiEditor.Extensions.Runtime;
  15. using PixiEditor.Helpers;
  16. using PixiEditor.IdentityProvider;
  17. using PixiEditor.Linux;
  18. using PixiEditor.MacOs;
  19. using PixiEditor.OperatingSystem;
  20. using PixiEditor.Platform;
  21. using PixiEditor.ViewModels;
  22. using PixiEditor.Windows;
  23. namespace PixiEditor.Tests;
  24. public class PixiEditorTest
  25. {
  26. public PixiEditorTest()
  27. {
  28. if (DrawingBackendApi.HasBackend)
  29. {
  30. return;
  31. }
  32. try
  33. {
  34. IRenderApi renderApi = new VulkanRenderApi();
  35. if (System.OperatingSystem.IsMacOS())
  36. {
  37. renderApi = new OpenGlRenderApi();
  38. }
  39. var engine = new DrawingEngine(renderApi, new GlfwWindowingPlatform(renderApi), new SkiaDrawingBackend(),
  40. new TestsRenderingDispatcher());
  41. var app = new TestingApp();
  42. Console.WriteLine("Running DrawieEngine with configuration:");
  43. Console.WriteLine($"\t- RenderApi: {engine.RenderApi}");
  44. Console.WriteLine($"\t- WindowingPlatform: {engine.RenderApi}");
  45. Console.WriteLine($"\t- DrawingBackend: {engine.RenderApi}");
  46. app.Initialize(engine);
  47. IWindow window = app.CreateMainWindow();
  48. window.Initialize();
  49. DrawingBackendApi.InitializeBackend(engine.RenderApi);
  50. app.Run();
  51. }
  52. catch (Exception ex)
  53. {
  54. if (!DrawingBackendApi.HasBackend)
  55. DrawingBackendApi.SetupBackend(new SkiaDrawingBackend(), new TestsRenderingDispatcher());
  56. }
  57. }
  58. }
  59. public class FullPixiEditorTest : PixiEditorTest
  60. {
  61. public FullPixiEditorTest()
  62. {
  63. ExtensionLoader loader = new ExtensionLoader(["TestExtensions"], "TestExtensions/Unpacked");
  64. if (IOperatingSystem.Current == null)
  65. {
  66. IOperatingSystem os;
  67. if (System.OperatingSystem.IsWindows())
  68. {
  69. os = new WindowsOperatingSystem();
  70. }
  71. else if (System.OperatingSystem.IsLinux())
  72. {
  73. os = new LinuxOperatingSystem();
  74. }
  75. else if (System.OperatingSystem.IsMacOS())
  76. {
  77. os = new MacOperatingSystem();
  78. }
  79. else
  80. {
  81. throw new NotSupportedException("Unsupported operating system");
  82. }
  83. IOperatingSystem.RegisterOS(os);
  84. }
  85. if (IPlatform.Current == null)
  86. {
  87. IPlatform.RegisterPlatform(new TestPlatform());
  88. }
  89. var services = new ServiceCollection()
  90. .AddPlatform()
  91. .AddPixiEditor(loader)
  92. .AddExtensionServices(loader)
  93. .BuildServiceProvider();
  94. var vm = services.GetRequiredService<ViewModelMain>();
  95. vm.Setup(services);
  96. }
  97. class TestPlatform : IPlatform
  98. {
  99. public string Id { get; } = "TestPlatform";
  100. public string Name { get; } = "Tests";
  101. public bool PerformHandshake()
  102. {
  103. return true;
  104. }
  105. public void Update()
  106. {
  107. }
  108. public IAdditionalContentProvider? AdditionalContentProvider { get; } = new NullAdditionalContentProvider();
  109. public IIdentityProvider? IdentityProvider { get; }
  110. }
  111. }
  112. public class TestingApp : DrawieApp
  113. {
  114. IWindow window;
  115. public override IWindow CreateMainWindow()
  116. {
  117. window = Engine.WindowingPlatform.CreateWindow("Testing app", VecI.One);
  118. return window;
  119. }
  120. protected override void OnInitialize()
  121. {
  122. window.IsVisible = false;
  123. }
  124. }
  125. class TestsRenderingDispatcher : IRenderingDispatcher
  126. {
  127. public Action<Action> Invoke { get; } = action => action();
  128. public Task<TResult> InvokeAsync<TResult>(Func<TResult> function)
  129. {
  130. return Task.FromResult(function());
  131. }
  132. public Task<TResult> InvokeInBackgroundAsync<TResult>(Func<TResult> function)
  133. {
  134. return Task.FromResult(function());
  135. }
  136. public Task InvokeInBackgroundAsync(Action function)
  137. {
  138. function();
  139. return Task.CompletedTask;
  140. }
  141. public Task InvokeAsync(Action function)
  142. {
  143. function();
  144. return Task.CompletedTask;
  145. }
  146. public IDisposable EnsureContext()
  147. {
  148. return new EmptyDisposable();
  149. }
  150. }