PixiEditorTest.cs 3.5 KB

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