// Copyright (c) The Avalonia Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. using System; using Avalonia.Input; using Avalonia.Layout; using Avalonia.Platform; using Avalonia.Styling; using Avalonia.Controls; using Avalonia.Rendering; namespace Avalonia.UnitTests { public class UnitTestApplication : Application { private readonly TestServices _services; public UnitTestApplication(TestServices services) { _services = services ?? new TestServices(); RegisterServices(); } public static new UnitTestApplication Current => (UnitTestApplication)Application.Current; public TestServices Services => _services; public static IDisposable Start(TestServices services = null) { var scope = AvaloniaLocator.EnterScope(); var app = new UnitTestApplication(services); AvaloniaLocator.CurrentMutable.BindToSelf(app); return scope; } public override void RegisterServices() { AvaloniaLocator.CurrentMutable .Bind().ToConstant(Services.AssetLoader) .Bind().ToConstant(Services.FocusManager) .BindToSelf(this) .Bind().ToConstant(Services.InputManager) .Bind().ToConstant(Services.KeyboardDevice?.Invoke()) .Bind().ToConstant(Services.LayoutManager) .Bind().ToConstant(Services.Platform) .Bind().ToConstant(new RendererFactory(Services.Renderer)) .Bind().ToConstant(Services.RenderInterface) .Bind().ToConstant(Services.RenderLoop) .Bind().ToConstant(Services.ThreadingInterface) .Bind().ToConstant(Services.StandardCursorFactory) .Bind().ToConstant(Services.Styler) .Bind().ToConstant(Services.WindowingPlatform) .Bind().ToConstant(this); var styles = Services.Theme?.Invoke(); if (styles != null) { Styles.AddRange(styles); } } private class RendererFactory : IRendererFactory { Func _func; public RendererFactory(Func func) { _func = func; } public IRenderer CreateRenderer(IRenderRoot root, IRenderLoop renderLoop) { return _func?.Invoke(root, renderLoop); } } } }