UnitTestApplication.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using Avalonia.Input;
  5. using Avalonia.Layout;
  6. using Avalonia.Platform;
  7. using Avalonia.Styling;
  8. using Avalonia.Controls;
  9. using Avalonia.Rendering;
  10. using Avalonia.Threading;
  11. using System.Reactive.Disposables;
  12. using System.Reactive.Concurrency;
  13. namespace Avalonia.UnitTests
  14. {
  15. public class UnitTestApplication : Application
  16. {
  17. private readonly TestServices _services;
  18. public UnitTestApplication(TestServices services)
  19. {
  20. _services = services ?? new TestServices();
  21. RegisterServices();
  22. }
  23. public static new UnitTestApplication Current => (UnitTestApplication)Application.Current;
  24. public TestServices Services => _services;
  25. public static IDisposable Start(TestServices services = null)
  26. {
  27. var scope = AvaloniaLocator.EnterScope();
  28. var app = new UnitTestApplication(services);
  29. AvaloniaLocator.CurrentMutable.BindToSelf<Application>(app);
  30. Dispatcher.UIThread.UpdateServices();
  31. return Disposable.Create(() =>
  32. {
  33. scope.Dispose();
  34. Dispatcher.UIThread.UpdateServices();
  35. });
  36. }
  37. public override void RegisterServices()
  38. {
  39. AvaloniaLocator.CurrentMutable
  40. .Bind<IAssetLoader>().ToConstant(Services.AssetLoader)
  41. .Bind<IFocusManager>().ToConstant(Services.FocusManager)
  42. .BindToSelf<IGlobalStyles>(this)
  43. .Bind<IInputManager>().ToConstant(Services.InputManager)
  44. .Bind<IKeyboardDevice>().ToConstant(Services.KeyboardDevice?.Invoke())
  45. .Bind<IKeyboardNavigationHandler>().ToConstant(Services.KeyboardNavigation)
  46. .Bind<ILayoutManager>().ToConstant(Services.LayoutManager)
  47. .Bind<IRuntimePlatform>().ToConstant(Services.Platform)
  48. .Bind<IRendererFactory>().ToConstant(new RendererFactory(Services.Renderer))
  49. .Bind<IPlatformRenderInterface>().ToConstant(Services.RenderInterface)
  50. .Bind<IRenderLoop>().ToConstant(Services.RenderLoop)
  51. .Bind<IPlatformThreadingInterface>().ToConstant(Services.ThreadingInterface)
  52. .Bind<IScheduler>().ToConstant(Services.Scheduler)
  53. .Bind<IStandardCursorFactory>().ToConstant(Services.StandardCursorFactory)
  54. .Bind<IStyler>().ToConstant(Services.Styler)
  55. .Bind<IWindowingPlatform>().ToConstant(Services.WindowingPlatform)
  56. .Bind<IApplicationLifecycle>().ToConstant(this);
  57. var styles = Services.Theme?.Invoke();
  58. if (styles != null)
  59. {
  60. Styles.AddRange(styles);
  61. }
  62. }
  63. private class RendererFactory : IRendererFactory
  64. {
  65. Func<IRenderRoot, IRenderLoop, IRenderer> _func;
  66. public RendererFactory(Func<IRenderRoot, IRenderLoop, IRenderer> func)
  67. {
  68. _func = func;
  69. }
  70. public IRenderer CreateRenderer(IRenderRoot root, IRenderLoop renderLoop)
  71. {
  72. return _func?.Invoke(root, renderLoop);
  73. }
  74. }
  75. }
  76. }