UnitTestApplication.cs 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using Avalonia.Input;
  3. using Avalonia.Layout;
  4. using Avalonia.Platform;
  5. using Avalonia.Styling;
  6. using Avalonia.Controls;
  7. using Avalonia.Rendering;
  8. using Avalonia.Threading;
  9. using System.Reactive.Disposables;
  10. using System.Reactive.Concurrency;
  11. using Avalonia.Input.Platform;
  12. using Avalonia.Animation;
  13. using Avalonia.Media;
  14. namespace Avalonia.UnitTests
  15. {
  16. public class UnitTestApplication : Application
  17. {
  18. private readonly TestServices _services;
  19. public UnitTestApplication() : this(null)
  20. {
  21. }
  22. public UnitTestApplication(TestServices services)
  23. {
  24. _services = services ?? new TestServices();
  25. AvaloniaLocator.CurrentMutable.BindToSelf<Application>(this);
  26. RegisterServices();
  27. }
  28. static UnitTestApplication()
  29. {
  30. AssetLoader.RegisterResUriParsers();
  31. }
  32. public static new UnitTestApplication Current => (UnitTestApplication)Application.Current;
  33. public TestServices Services => _services;
  34. public static IDisposable Start(TestServices services = null)
  35. {
  36. var scope = AvaloniaLocator.EnterScope();
  37. var app = new UnitTestApplication(services);
  38. Dispatcher.UIThread.UpdateServices();
  39. return Disposable.Create(() =>
  40. {
  41. if (Dispatcher.UIThread.CheckAccess())
  42. {
  43. Dispatcher.UIThread.RunJobs();
  44. }
  45. scope.Dispose();
  46. Dispatcher.UIThread.UpdateServices();
  47. });
  48. }
  49. public override void RegisterServices()
  50. {
  51. AvaloniaLocator.CurrentMutable
  52. .Bind<IAssetLoader>().ToConstant(Services.AssetLoader)
  53. .Bind<IFocusManager>().ToConstant(Services.FocusManager)
  54. .Bind<IGlobalClock>().ToConstant(Services.GlobalClock)
  55. .BindToSelf<IGlobalStyles>(this)
  56. .Bind<IInputManager>().ToConstant(Services.InputManager)
  57. .Bind<IKeyboardDevice>().ToConstant(Services.KeyboardDevice?.Invoke())
  58. .Bind<IKeyboardNavigationHandler>().ToConstant(Services.KeyboardNavigation)
  59. .Bind<IMouseDevice>().ToConstant(Services.MouseDevice?.Invoke())
  60. .Bind<IRuntimePlatform>().ToConstant(Services.Platform)
  61. .Bind<IPlatformRenderInterface>().ToConstant(Services.RenderInterface)
  62. .Bind<IFontManagerImpl>().ToConstant(Services.FontManagerImpl)
  63. .Bind<ITextShaperImpl>().ToConstant(Services.TextShaperImpl)
  64. .Bind<IPlatformThreadingInterface>().ToConstant(Services.ThreadingInterface)
  65. .Bind<ICursorFactory>().ToConstant(Services.StandardCursorFactory)
  66. .Bind<IWindowingPlatform>().ToConstant(Services.WindowingPlatform)
  67. .Bind<PlatformHotkeyConfiguration>().ToSingleton<PlatformHotkeyConfiguration>();
  68. // This is a hack to make tests work, we need to refactor the way font manager is registered
  69. // See https://github.com/AvaloniaUI/Avalonia/issues/10081
  70. AvaloniaLocator.CurrentMutable.Bind<FontManager>().ToConstant((FontManager)null!);
  71. var theme = Services.Theme?.Invoke();
  72. if (theme is Style styles)
  73. {
  74. Styles.AddRange(styles.Children);
  75. }
  76. else if (theme is not null)
  77. {
  78. Styles.Add(theme);
  79. }
  80. }
  81. }
  82. }