UnitTestApplication.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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<IMouseDevice>().ToConstant(Services.MouseDevice?.Invoke())
  47. .Bind<IRuntimePlatform>().ToConstant(Services.Platform)
  48. .Bind<IPlatformRenderInterface>().ToConstant(Services.RenderInterface)
  49. .Bind<IPlatformThreadingInterface>().ToConstant(Services.ThreadingInterface)
  50. .Bind<IScheduler>().ToConstant(Services.Scheduler)
  51. .Bind<IStandardCursorFactory>().ToConstant(Services.StandardCursorFactory)
  52. .Bind<IStyler>().ToConstant(Services.Styler)
  53. .Bind<IWindowingPlatform>().ToConstant(Services.WindowingPlatform)
  54. .Bind<IApplicationLifecycle>().ToConstant(this);
  55. var styles = Services.Theme?.Invoke();
  56. if (styles != null)
  57. {
  58. Styles.AddRange(styles);
  59. }
  60. }
  61. }
  62. }