PixiEditorTest.cs 4.5 KB

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