UnitTestApplication.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. scope.Dispose();
  42. Dispatcher.UIThread.UpdateServices();
  43. });
  44. }
  45. public override void RegisterServices()
  46. {
  47. AvaloniaLocator.CurrentMutable
  48. .Bind<IAssetLoader>().ToConstant(Services.AssetLoader)
  49. .Bind<IFocusManager>().ToConstant(Services.FocusManager)
  50. .Bind<IGlobalClock>().ToConstant(Services.GlobalClock)
  51. .BindToSelf<IGlobalStyles>(this)
  52. .Bind<IInputManager>().ToConstant(Services.InputManager)
  53. .Bind<IKeyboardDevice>().ToConstant(Services.KeyboardDevice?.Invoke())
  54. .Bind<IKeyboardNavigationHandler>().ToConstant(Services.KeyboardNavigation)
  55. .Bind<IMouseDevice>().ToConstant(Services.MouseDevice?.Invoke())
  56. .Bind<IRuntimePlatform>().ToConstant(Services.Platform)
  57. .Bind<IPlatformRenderInterface>().ToConstant(Services.RenderInterface)
  58. .Bind<IFontManagerImpl>().ToConstant(Services.FontManagerImpl)
  59. .Bind<ITextShaperImpl>().ToConstant(Services.TextShaperImpl)
  60. .Bind<IPlatformThreadingInterface>().ToConstant(Services.ThreadingInterface)
  61. .Bind<ICursorFactory>().ToConstant(Services.StandardCursorFactory)
  62. .Bind<IWindowingPlatform>().ToConstant(Services.WindowingPlatform)
  63. .Bind<PlatformHotkeyConfiguration>().ToSingleton<PlatformHotkeyConfiguration>();
  64. // This is a hack to make tests work, we need to refactor the way font manager is registered
  65. // See https://github.com/AvaloniaUI/Avalonia/issues/10081
  66. AvaloniaLocator.CurrentMutable.Bind<FontManager>().ToConstant((FontManager)null!);
  67. var theme = Services.Theme?.Invoke();
  68. if (theme is Style styles)
  69. {
  70. Styles.AddRange(styles.Children);
  71. }
  72. else if (theme is not null)
  73. {
  74. Styles.Add(theme);
  75. }
  76. }
  77. }
  78. }