// Copyright (c) .NET Foundation. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System; using System.Collections.Generic; using System.Runtime.ExceptionServices; using System.Text.Encodings.Web; using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.Web.Rendering; using Microsoft.AspNetCore.Components.Rendering; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging.Abstractions; using Moq; namespace Microsoft.AspNetCore.Components.Server.Circuits { internal class TestCircuitHost : CircuitHost { private TestCircuitHost(string circuitId, IServiceScope scope, CircuitClientProxy client, RendererRegistry rendererRegistry, RemoteRenderer renderer, IList descriptors, IDispatcher dispatcher, RemoteJSRuntime jsRuntime, CircuitHandler[] circuitHandlers, ILogger logger) : base(circuitId, scope, client, rendererRegistry, renderer, descriptors, dispatcher, jsRuntime, circuitHandlers, logger) { } protected override void OnHandlerError(CircuitHandler circuitHandler, string handlerMethod, Exception ex) { ExceptionDispatchInfo.Capture(ex).Throw(); } public static CircuitHost Create( string circuitId = null, IServiceScope serviceScope = null, RemoteRenderer remoteRenderer = null, CircuitHandler[] handlers = null, CircuitClientProxy clientProxy = null) { serviceScope = serviceScope ?? Mock.Of(); clientProxy = clientProxy ?? new CircuitClientProxy(Mock.Of(), Guid.NewGuid().ToString()); var renderRegistry = new RendererRegistry(); var jsRuntime = new RemoteJSRuntime(); var dispatcher = Rendering.Renderer.CreateDefaultDispatcher(); if (remoteRenderer == null) { remoteRenderer = new RemoteRenderer( serviceScope.ServiceProvider ?? Mock.Of(), new RendererRegistry(), jsRuntime, clientProxy, dispatcher, HtmlEncoder.Default, NullLogger.Instance); } handlers = handlers ?? Array.Empty(); return new TestCircuitHost( circuitId ?? Guid.NewGuid().ToString(), serviceScope, clientProxy, renderRegistry, remoteRenderer, new List(), dispatcher, jsRuntime, handlers, NullLogger.Instance); } } }