// 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.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Rendering; namespace Microsoft.AspNetCore.Components.Test.Helpers { public class TestRenderer : Renderer { public TestRenderer(): this(new TestServiceProvider()) { } public TestRenderer(IServiceProvider serviceProvider) : base(serviceProvider) { } public Action OnUpdateDisplay { get; set; } public List Batches { get; } = new List(); public new int AssignRootComponentId(IComponent component) => base.AssignRootComponentId(component); public new void RenderRootComponent(int componentId) => base.RenderRootComponent(componentId); public new void DispatchEvent(int componentId, int eventHandlerId, UIEventArgs args) => base.DispatchEvent(componentId, eventHandlerId, args); public T InstantiateComponent() where T : IComponent => (T)InstantiateComponent(typeof(T)); protected override Task UpdateDisplayAsync(in RenderBatch renderBatch) { OnUpdateDisplay?.Invoke(renderBatch); var capturedBatch = new CapturedBatch(); Batches.Add(capturedBatch); for (var i = 0; i < renderBatch.UpdatedComponents.Count; i++) { ref var renderTreeDiff = ref renderBatch.UpdatedComponents.Array[i]; capturedBatch.AddDiff(renderTreeDiff); } // Clone other data, as underlying storage will get reused by later batches capturedBatch.ReferenceFrames = renderBatch.ReferenceFrames.ToArray(); capturedBatch.DisposedComponentIDs = renderBatch.DisposedComponentIDs.ToList(); // This renderer updates the UI synchronously, like the WebAssembly one. // To test async UI updates, subclass TestRenderer and override UpdateDisplayAsync. return Task.CompletedTask; } } }