PixiEditorTest.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using Drawie.Backend.Core.Bridge;
  2. using Drawie.Numerics;
  3. using Drawie.Skia;
  4. using Drawie.Windowing;
  5. using DrawiEngine;
  6. using DrawiEngine.Desktop;
  7. using Microsoft.Extensions.DependencyInjection;
  8. using PixiEditor.Extensions.Runtime;
  9. using PixiEditor.Helpers;
  10. using PixiEditor.Linux;
  11. using PixiEditor.MacOs;
  12. using PixiEditor.OperatingSystem;
  13. using PixiEditor.Platform;
  14. using PixiEditor.ViewModels;
  15. using PixiEditor.Windows;
  16. namespace PixiEditor.Tests;
  17. public class PixiEditorTest
  18. {
  19. public PixiEditorTest()
  20. {
  21. if (DrawingBackendApi.HasBackend)
  22. {
  23. return;
  24. }
  25. try
  26. {
  27. var engine = DesktopDrawingEngine.CreateDefaultDesktop();
  28. var app = new TestingApp();
  29. Console.WriteLine("Running DrawieEngine with configuration:");
  30. Console.WriteLine($"\t- RenderApi: {engine.RenderApi}");
  31. Console.WriteLine($"\t- WindowingPlatform: {engine.RenderApi}");
  32. Console.WriteLine($"\t- DrawingBackend: {engine.RenderApi}");
  33. app.Initialize(engine);
  34. IWindow window = app.CreateMainWindow();
  35. window.Initialize();
  36. DrawingBackendApi.InitializeBackend(engine.RenderApi);
  37. app.Run();
  38. }
  39. catch (Exception ex)
  40. {
  41. if (!DrawingBackendApi.HasBackend)
  42. DrawingBackendApi.SetupBackend(new SkiaDrawingBackend(), new DrawieRenderingDispatcher());
  43. }
  44. }
  45. }
  46. public class FullPixiEditorTest : PixiEditorTest
  47. {
  48. public FullPixiEditorTest()
  49. {
  50. ExtensionLoader loader = new ExtensionLoader(["TestExtensions"], "TestExtensions/Unpacked");
  51. if (IOperatingSystem.Current == null)
  52. {
  53. IOperatingSystem os;
  54. if (System.OperatingSystem.IsWindows())
  55. {
  56. os = new WindowsOperatingSystem();
  57. }
  58. else if (System.OperatingSystem.IsLinux())
  59. {
  60. os = new LinuxOperatingSystem();
  61. }
  62. else if (System.OperatingSystem.IsMacOS())
  63. {
  64. os = new MacOperatingSystem();
  65. }
  66. else
  67. {
  68. throw new NotSupportedException("Unsupported operating system");
  69. }
  70. IOperatingSystem.RegisterOS(os);
  71. }
  72. if (IPlatform.Current == null)
  73. {
  74. IPlatform.RegisterPlatform(new TestPlatform());
  75. }
  76. var services = new ServiceCollection()
  77. .AddPlatform()
  78. .AddPixiEditor(loader)
  79. .AddExtensionServices(loader)
  80. .BuildServiceProvider();
  81. var vm = services.GetRequiredService<ViewModelMain>();
  82. vm.Setup(services);
  83. }
  84. class TestPlatform : IPlatform
  85. {
  86. public string Id { get; } = "TestPlatform";
  87. public string Name { get; } = "Tests";
  88. public bool PerformHandshake()
  89. {
  90. return true;
  91. }
  92. public void Update()
  93. {
  94. }
  95. public IAdditionalContentProvider? AdditionalContentProvider { get; } = new NullAdditionalContentProvider();
  96. }
  97. }
  98. public class TestingApp : DrawieApp
  99. {
  100. IWindow window;
  101. public override IWindow CreateMainWindow()
  102. {
  103. window = Engine.WindowingPlatform.CreateWindow("Testing app", VecI.One);
  104. return window;
  105. }
  106. protected override void OnInitialize()
  107. {
  108. window.IsVisible = false;
  109. }
  110. }