UnitTestApplication.cs 3.9 KB

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