UnitTestApplication.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. using System;
  2. using Avalonia.Input;
  3. using Avalonia.Platform;
  4. using Avalonia.Styling;
  5. using Avalonia.Controls;
  6. using Avalonia.Threading;
  7. using System.Reactive.Disposables;
  8. using System.Threading;
  9. using Avalonia.Input.Platform;
  10. using Avalonia.Animation;
  11. using Avalonia.Media;
  12. namespace Avalonia.UnitTests
  13. {
  14. public class UnitTestApplication : Application
  15. {
  16. private readonly TestServices _services;
  17. public UnitTestApplication() : this(null)
  18. {
  19. }
  20. public UnitTestApplication(TestServices services)
  21. {
  22. _services = services ?? new TestServices();
  23. AvaloniaLocator.CurrentMutable.BindToSelf<Application>(this);
  24. RegisterServices();
  25. }
  26. static UnitTestApplication()
  27. {
  28. AssetLoader.RegisterResUriParsers();
  29. }
  30. public static new UnitTestApplication Current => (UnitTestApplication)Application.Current;
  31. public TestServices Services => _services;
  32. public static IDisposable Start(TestServices services = null)
  33. {
  34. var scope = AvaloniaLocator.EnterScope();
  35. var oldContext = SynchronizationContext.Current;
  36. _ = new UnitTestApplication(services);
  37. Dispatcher.ResetBeforeUnitTests();
  38. return Disposable.Create(() =>
  39. {
  40. if (Dispatcher.UIThread.CheckAccess())
  41. {
  42. Dispatcher.UIThread.RunJobs();
  43. }
  44. ((ToolTipService)AvaloniaLocator.Current.GetService<IToolTipService>())?.Dispose();
  45. (AvaloniaLocator.Current.GetService<FontManager>() as IDisposable)?.Dispose();
  46. Dispatcher.ResetForUnitTests();
  47. scope.Dispose();
  48. Dispatcher.ResetBeforeUnitTests();
  49. SynchronizationContext.SetSynchronizationContext(oldContext);
  50. });
  51. }
  52. public override void RegisterServices()
  53. {
  54. AvaloniaLocator.CurrentMutable
  55. .Bind<IAssetLoader>().ToConstant(Services.AssetLoader)
  56. .Bind<IGlobalClock>().ToConstant(Services.GlobalClock)
  57. .BindToSelf<IGlobalStyles>(this)
  58. .Bind<IInputManager>().ToConstant(Services.InputManager)
  59. .Bind<IToolTipService>().ToConstant(Services.InputManager == null ? null : new ToolTipService(Services.InputManager))
  60. .Bind<IKeyboardDevice>().ToConstant(Services.KeyboardDevice?.Invoke())
  61. .Bind<IMouseDevice>().ToConstant(Services.MouseDevice?.Invoke())
  62. .Bind<IKeyboardNavigationHandler>().ToFunc(Services.KeyboardNavigation ?? (() => null))
  63. .Bind<IRuntimePlatform>().ToConstant(Services.Platform)
  64. .Bind<IPlatformRenderInterface>().ToConstant(Services.RenderInterface)
  65. .Bind<IFontManagerImpl>().ToConstant(Services.FontManagerImpl)
  66. .Bind<ITextShaperImpl>().ToConstant(Services.TextShaperImpl)
  67. .Bind<IDispatcherImpl>().ToConstant(Services.DispatcherImpl)
  68. .Bind<ICursorFactory>().ToConstant(Services.StandardCursorFactory)
  69. .Bind<IWindowingPlatform>().ToConstant(Services.WindowingPlatform)
  70. .Bind<PlatformHotkeyConfiguration>().ToSingleton<PlatformHotkeyConfiguration>()
  71. .Bind<IPlatformSettings>().ToSingleton<DefaultPlatformSettings>()
  72. .Bind<IAccessKeyHandler>().ToConstant(Services.AccessKeyHandler)
  73. ;
  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. }