Reorganise some test helpers in shared locations

This commit is contained in:
Steve Sanderson 2018-02-16 16:14:36 +00:00
parent 96c6d959c0
commit df825de86d
10 changed files with 85 additions and 65 deletions

View File

@ -18,7 +18,7 @@
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Blazor.Build\Microsoft.AspNetCore.Blazor.Build.csproj" />
<!-- Shared sources -->
<Compile Include="..\shared\AssertFrame.cs" />
<Compile Include="..\shared\AssertFrame.cs" Link="Helpers\AssertFrame.cs" />
</ItemGroup>
<ItemGroup>

View File

@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Blazor.Components;
using Microsoft.AspNetCore.Blazor.Layouts;
using Microsoft.AspNetCore.Blazor.Rendering;
using Microsoft.AspNetCore.Blazor.RenderTree;
using Microsoft.AspNetCore.Blazor.Test.Shared;
using Microsoft.AspNetCore.Blazor.Test.Helpers;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using System;

View File

@ -4,7 +4,7 @@
using Microsoft.AspNetCore.Blazor.Components;
using Microsoft.AspNetCore.Blazor.RenderTree;
namespace Microsoft.AspNetCore.Blazor.Test
namespace Microsoft.AspNetCore.Blazor.Test.Helpers
{
public abstract class AutoRenderComponent : IComponent
{

View File

@ -0,0 +1,37 @@
// 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 Microsoft.AspNetCore.Blazor.RenderTree;
namespace Microsoft.AspNetCore.Blazor.Test
{
public class CapturedBatch
{
public IDictionary<int, List<RenderTreeDiff>> DiffsByComponentId { get; }
= new Dictionary<int, List<RenderTreeDiff>>();
public IList<RenderTreeDiff> DiffsInOrder { get; }
= new List<RenderTreeDiff>();
public IList<int> DisposedComponentIDs { get; set; }
public RenderTreeFrame[] ReferenceFrames { get; set; }
internal void AddDiff(RenderTreeDiff diff)
{
var componentId = diff.ComponentId;
if (!DiffsByComponentId.ContainsKey(componentId))
{
DiffsByComponentId.Add(componentId, new List<RenderTreeDiff>());
}
// Clone the diff, because its underlying storage will get reused in subsequent batches
var diffClone = new RenderTreeDiff(
diff.ComponentId,
new ArraySegment<RenderTreeEdit>(diff.Edits.ToArray()));
DiffsByComponentId[componentId].Add(diffClone);
DiffsInOrder.Add(diffClone);
}
}
}

View File

@ -0,0 +1,39 @@
// 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.Collections.Generic;
using System.Linq;
using Microsoft.AspNetCore.Blazor.Components;
using Microsoft.AspNetCore.Blazor.Rendering;
using Microsoft.AspNetCore.Blazor.RenderTree;
namespace Microsoft.AspNetCore.Blazor.Test.Helpers
{
public class TestRenderer : Renderer
{
public List<CapturedBatch> Batches { get; }
= new List<CapturedBatch>();
public new int AssignComponentId(IComponent component)
=> base.AssignComponentId(component);
public new void DispatchEvent(int componentId, int eventHandlerId, UIEventArgs args)
=> base.DispatchEvent(componentId, eventHandlerId, args);
protected override void UpdateDisplay(RenderBatch 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();
}
}
}

View File

@ -17,7 +17,7 @@
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.Blazor\Microsoft.AspNetCore.Blazor.csproj" />
<!-- Shared sources -->
<Compile Include="..\shared\AssertFrame.cs" />
<Compile Include="..\shared\AssertFrame.cs" Link="Helpers\AssertFrame.cs" />
</ItemGroup>
</Project>

View File

@ -4,7 +4,7 @@
using Microsoft.AspNetCore.Blazor.Components;
using Microsoft.AspNetCore.Blazor.Rendering;
using Microsoft.AspNetCore.Blazor.RenderTree;
using Microsoft.AspNetCore.Blazor.Test.Shared;
using Microsoft.AspNetCore.Blazor.Test.Helpers;
using System;
using System.Linq;
using Xunit;

View File

@ -4,7 +4,7 @@
using Microsoft.AspNetCore.Blazor.Components;
using Microsoft.AspNetCore.Blazor.Rendering;
using Microsoft.AspNetCore.Blazor.RenderTree;
using Microsoft.AspNetCore.Blazor.Test.Shared;
using Microsoft.AspNetCore.Blazor.Test.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;

View File

@ -7,12 +7,12 @@ using System.Linq;
using Microsoft.AspNetCore.Blazor.Components;
using Microsoft.AspNetCore.Blazor.Rendering;
using Microsoft.AspNetCore.Blazor.RenderTree;
using Microsoft.AspNetCore.Blazor.Test.Shared;
using Microsoft.AspNetCore.Blazor.Test.Helpers;
using Xunit;
namespace Microsoft.AspNetCore.Blazor.Test
{
public partial class RendererTest
public class RendererTest
{
[Fact]
public void CanRenderTopLevelComponents()
@ -873,62 +873,6 @@ namespace Microsoft.AspNetCore.Blazor.Test
}
}
private class TestRenderer : Renderer
{
public List<CapturedBatch> Batches { get; }
= new List<CapturedBatch>();
public new int AssignComponentId(IComponent component)
=> base.AssignComponentId(component);
public new void DispatchEvent(int componentId, int eventHandlerId, UIEventArgs args)
=> base.DispatchEvent(componentId, eventHandlerId, args);
protected override void UpdateDisplay(RenderBatch 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();
}
}
private class CapturedBatch
{
public IDictionary<int, List<RenderTreeDiff>> DiffsByComponentId { get; }
= new Dictionary<int, List<RenderTreeDiff>>();
public IList<RenderTreeDiff> DiffsInOrder { get; }
= new List<RenderTreeDiff>();
public IList<int> DisposedComponentIDs { get; set; }
public RenderTreeFrame[] ReferenceFrames { get; set; }
internal void AddDiff(RenderTreeDiff diff)
{
var componentId = diff.ComponentId;
if (!DiffsByComponentId.ContainsKey(componentId))
{
DiffsByComponentId.Add(componentId, new List<RenderTreeDiff>());
}
// Clone the diff, because its underlying storage will get reused in subsequent batches
var diffClone = new RenderTreeDiff(
diff.ComponentId,
new ArraySegment<RenderTreeEdit>(diff.Edits.ToArray()));
DiffsByComponentId[componentId].Add(diffClone);
DiffsInOrder.Add(diffClone);
}
}
private class TestComponent : IComponent
{
private RenderHandle _renderHandle;

View File

@ -6,7 +6,7 @@ using Microsoft.AspNetCore.Blazor.Components;
using Microsoft.AspNetCore.Blazor.RenderTree;
using Xunit;
namespace Microsoft.AspNetCore.Blazor.Test.Shared
namespace Microsoft.AspNetCore.Blazor.Test.Helpers
{
internal static class AssertFrame
{