TestRenderer.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Copyright (c) .NET Foundation. All rights reserved.
  2. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Runtime.ExceptionServices;
  7. using System.Threading.Tasks;
  8. using Microsoft.AspNetCore.Components.Rendering;
  9. using Microsoft.Extensions.Logging.Abstractions;
  10. using Xunit;
  11. namespace Microsoft.AspNetCore.Components.Test.Helpers
  12. {
  13. public class TestRenderer : Renderer
  14. {
  15. public TestRenderer() : this(new TestServiceProvider())
  16. {
  17. }
  18. public TestRenderer(Dispatcher dispatcher) : base(new TestServiceProvider(), NullLoggerFactory.Instance)
  19. {
  20. Dispatcher = dispatcher;
  21. }
  22. public TestRenderer(IServiceProvider serviceProvider) : base(serviceProvider, NullLoggerFactory.Instance)
  23. {
  24. Dispatcher = Dispatcher.CreateDefault();
  25. }
  26. public override Dispatcher Dispatcher { get; }
  27. public Action OnExceptionHandled { get; set; }
  28. public Action<RenderBatch> OnUpdateDisplay { get; set; }
  29. public Action OnUpdateDisplayComplete { get; set; }
  30. public List<CapturedBatch> Batches { get; }
  31. = new List<CapturedBatch>();
  32. public List<Exception> HandledExceptions { get; } = new List<Exception>();
  33. public bool ShouldHandleExceptions { get; set; }
  34. public Task NextRenderResultTask { get; set; } = Task.CompletedTask;
  35. public new int AssignRootComponentId(IComponent component)
  36. => base.AssignRootComponentId(component);
  37. public void RenderRootComponent(int componentId, ParameterCollection? parameters = default)
  38. {
  39. var task = Dispatcher.InvokeAsync(() => base.RenderRootComponentAsync(componentId, parameters ?? ParameterCollection.Empty));
  40. UnwrapTask(task);
  41. }
  42. public new Task RenderRootComponentAsync(int componentId)
  43. => Dispatcher.InvokeAsync(() => base.RenderRootComponentAsync(componentId));
  44. public new Task RenderRootComponentAsync(int componentId, ParameterCollection parameters)
  45. => Dispatcher.InvokeAsync(() => base.RenderRootComponentAsync(componentId, parameters));
  46. public Task DispatchEventAsync(ulong eventHandlerId, UIEventArgs args)
  47. => Dispatcher.InvokeAsync(() => base.DispatchEventAsync(eventHandlerId, null, args));
  48. public new Task DispatchEventAsync(ulong eventHandlerId, EventFieldInfo eventFieldInfo, UIEventArgs args)
  49. => Dispatcher.InvokeAsync(() => base.DispatchEventAsync(eventHandlerId, eventFieldInfo, args));
  50. private static Task UnwrapTask(Task task)
  51. {
  52. // This should always be run synchronously
  53. Assert.True(task.IsCompleted);
  54. if (task.IsFaulted)
  55. {
  56. var exception = task.Exception.Flatten().InnerException;
  57. while (exception is AggregateException e)
  58. {
  59. exception = e.InnerException;
  60. }
  61. ExceptionDispatchInfo.Capture(exception).Throw();
  62. }
  63. return task;
  64. }
  65. public T InstantiateComponent<T>() where T : IComponent
  66. => (T)InstantiateComponent(typeof(T));
  67. protected override void HandleException(Exception exception)
  68. {
  69. if (!ShouldHandleExceptions)
  70. {
  71. ExceptionDispatchInfo.Capture(exception).Throw();
  72. }
  73. HandledExceptions.Add(exception);
  74. OnExceptionHandled?.Invoke();
  75. }
  76. protected override Task UpdateDisplayAsync(in RenderBatch renderBatch)
  77. {
  78. OnUpdateDisplay?.Invoke(renderBatch);
  79. var capturedBatch = new CapturedBatch();
  80. Batches.Add(capturedBatch);
  81. for (var i = 0; i < renderBatch.UpdatedComponents.Count; i++)
  82. {
  83. ref var renderTreeDiff = ref renderBatch.UpdatedComponents.Array[i];
  84. capturedBatch.AddDiff(renderTreeDiff);
  85. }
  86. // Clone other data, as underlying storage will get reused by later batches
  87. capturedBatch.ReferenceFrames = renderBatch.ReferenceFrames.AsEnumerable().ToArray();
  88. capturedBatch.DisposedComponentIDs = renderBatch.DisposedComponentIDs.AsEnumerable().ToList();
  89. // This renderer updates the UI synchronously, like the WebAssembly one.
  90. // To test async UI updates, subclass TestRenderer and override UpdateDisplayAsync.
  91. OnUpdateDisplayComplete?.Invoke();
  92. return NextRenderResultTask;
  93. }
  94. }
  95. }