Browse Source
Conflicts: src/Avalonia.Layout/LayoutManager.cs src/Avalonia.Layout/Layoutable.cs tests/Avalonia.Layout.UnitTests/LayoutManagerTests.cs tests/Avalonia.Layout.UnitTests/TestLayoutRoot.cspull/1014/head
29 changed files with 618 additions and 107 deletions
@ -1,5 +1,5 @@ |
|||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> |
||||
<ItemGroup> |
<ItemGroup> |
||||
<PackageReference Include="Moq" Version="4.7.1" /> |
<PackageReference Include="Moq" Version="4.7.25" /> |
||||
</ItemGroup> |
</ItemGroup> |
||||
</Project> |
</Project> |
||||
|
|||||
@ -0,0 +1,65 @@ |
|||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using Avalonia.Controls; |
||||
|
using Avalonia.Layout; |
||||
|
using Avalonia.UnitTests; |
||||
|
using BenchmarkDotNet.Attributes; |
||||
|
|
||||
|
namespace Avalonia.Benchmarks.Layout |
||||
|
{ |
||||
|
[MemoryDiagnoser] |
||||
|
public class Measure : IDisposable |
||||
|
{ |
||||
|
private IDisposable _app; |
||||
|
private TestRoot root; |
||||
|
private List<Control> controls = new List<Control>(); |
||||
|
|
||||
|
public Measure() |
||||
|
{ |
||||
|
_app = UnitTestApplication.Start(TestServices.RealLayoutManager); |
||||
|
|
||||
|
var panel = new StackPanel(); |
||||
|
root = new TestRoot { Child = panel }; |
||||
|
controls.Add(panel); |
||||
|
CreateChildren(panel, 3, 5); |
||||
|
LayoutManager.Instance.ExecuteInitialLayoutPass(root); |
||||
|
} |
||||
|
|
||||
|
public void Dispose() |
||||
|
{ |
||||
|
_app.Dispose(); |
||||
|
} |
||||
|
|
||||
|
[Benchmark] |
||||
|
public void Remeasure_Half() |
||||
|
{ |
||||
|
var random = new Random(1); |
||||
|
|
||||
|
foreach (var control in controls) |
||||
|
{ |
||||
|
if (random.Next(2) == 0) |
||||
|
{ |
||||
|
control.InvalidateMeasure(); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
LayoutManager.Instance.ExecuteLayoutPass(); |
||||
|
} |
||||
|
|
||||
|
private void CreateChildren(IPanel parent, int childCount, int iterations) |
||||
|
{ |
||||
|
for (var i = 0; i < childCount; ++i) |
||||
|
{ |
||||
|
var control = new StackPanel(); |
||||
|
parent.Children.Add(control); |
||||
|
|
||||
|
if (iterations > 0) |
||||
|
{ |
||||
|
CreateChildren(control, childCount, iterations - 1); |
||||
|
} |
||||
|
|
||||
|
controls.Add(control); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,29 @@ |
|||||
|
using System; |
||||
|
using Avalonia.Controls; |
||||
|
|
||||
|
namespace Avalonia.Layout.UnitTests |
||||
|
{ |
||||
|
internal class LayoutTestControl : Decorator |
||||
|
{ |
||||
|
public bool Measured { get; set; } |
||||
|
public bool Arranged { get; set; } |
||||
|
public Func<ILayoutable, Size, Size> DoMeasureOverride { get; set; } |
||||
|
public Func<ILayoutable, Size, Size> DoArrangeOverride { get; set; } |
||||
|
|
||||
|
protected override Size MeasureOverride(Size availableSize) |
||||
|
{ |
||||
|
Measured = true; |
||||
|
return DoMeasureOverride != null ? |
||||
|
DoMeasureOverride(this, availableSize) : |
||||
|
base.MeasureOverride(availableSize); |
||||
|
} |
||||
|
|
||||
|
protected override Size ArrangeOverride(Size finalSize) |
||||
|
{ |
||||
|
Arranged = true; |
||||
|
return DoArrangeOverride != null ? |
||||
|
DoArrangeOverride(this, finalSize) : |
||||
|
base.ArrangeOverride(finalSize); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,43 @@ |
|||||
|
// 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.UnitTests; |
||||
|
|
||||
|
namespace Avalonia.Layout.UnitTests |
||||
|
{ |
||||
|
internal class LayoutTestRoot : TestRoot, ILayoutable |
||||
|
{ |
||||
|
public bool Measured { get; set; } |
||||
|
public bool Arranged { get; set; } |
||||
|
public Func<ILayoutable, Size, Size> DoMeasureOverride { get; set; } |
||||
|
public Func<ILayoutable, Size, Size> DoArrangeOverride { get; set; } |
||||
|
|
||||
|
void ILayoutable.Measure(Size availableSize) |
||||
|
{ |
||||
|
Measured = true; |
||||
|
Measure(availableSize); |
||||
|
} |
||||
|
|
||||
|
void ILayoutable.Arrange(Rect rect) |
||||
|
{ |
||||
|
Arranged = true; |
||||
|
Arrange(rect); |
||||
|
} |
||||
|
|
||||
|
protected override Size MeasureOverride(Size availableSize) |
||||
|
{ |
||||
|
return DoMeasureOverride != null ? |
||||
|
DoMeasureOverride(this, availableSize) : |
||||
|
base.MeasureOverride(availableSize); |
||||
|
} |
||||
|
|
||||
|
protected override Size ArrangeOverride(Size finalSize) |
||||
|
{ |
||||
|
Arranged = true; |
||||
|
return DoArrangeOverride != null ? |
||||
|
DoArrangeOverride(this, finalSize) : |
||||
|
base.ArrangeOverride(finalSize); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,90 @@ |
|||||
|
using System; |
||||
|
using Avalonia.Controls; |
||||
|
using Moq; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Avalonia.Layout.UnitTests |
||||
|
{ |
||||
|
public class LayoutableTests |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Only_Calls_LayoutManager_InvalidateMeasure_Once() |
||||
|
{ |
||||
|
var target = new Mock<ILayoutManager>(); |
||||
|
|
||||
|
using (Start(target.Object)) |
||||
|
{ |
||||
|
var control = new Decorator(); |
||||
|
var root = new LayoutTestRoot { Child = control }; |
||||
|
|
||||
|
root.Measure(Size.Infinity); |
||||
|
root.Arrange(new Rect(root.DesiredSize)); |
||||
|
target.ResetCalls(); |
||||
|
|
||||
|
control.InvalidateMeasure(); |
||||
|
control.InvalidateMeasure(); |
||||
|
|
||||
|
target.Verify(x => x.InvalidateMeasure(control), Times.Once()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Only_Calls_LayoutManager_InvalidateArrange_Once() |
||||
|
{ |
||||
|
var target = new Mock<ILayoutManager>(); |
||||
|
|
||||
|
using (Start(target.Object)) |
||||
|
{ |
||||
|
var control = new Decorator(); |
||||
|
var root = new LayoutTestRoot { Child = control }; |
||||
|
|
||||
|
root.Measure(Size.Infinity); |
||||
|
root.Arrange(new Rect(root.DesiredSize)); |
||||
|
target.ResetCalls(); |
||||
|
|
||||
|
control.InvalidateArrange(); |
||||
|
control.InvalidateArrange(); |
||||
|
|
||||
|
target.Verify(x => x.InvalidateArrange(control), Times.Once()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Attaching_Control_To_Tree_Invalidates_Parent_Measure() |
||||
|
{ |
||||
|
var target = new Mock<ILayoutManager>(); |
||||
|
|
||||
|
using (Start(target.Object)) |
||||
|
{ |
||||
|
var control = new Decorator(); |
||||
|
var root = new LayoutTestRoot { Child = control }; |
||||
|
|
||||
|
root.Measure(Size.Infinity); |
||||
|
root.Arrange(new Rect(root.DesiredSize)); |
||||
|
Assert.True(control.IsMeasureValid); |
||||
|
|
||||
|
root.Child = null; |
||||
|
root.Measure(Size.Infinity); |
||||
|
root.Arrange(new Rect(root.DesiredSize)); |
||||
|
|
||||
|
Assert.False(control.IsMeasureValid); |
||||
|
Assert.True(root.IsMeasureValid); |
||||
|
|
||||
|
target.ResetCalls(); |
||||
|
|
||||
|
root.Child = control; |
||||
|
|
||||
|
Assert.False(root.IsMeasureValid); |
||||
|
Assert.False(control.IsMeasureValid); |
||||
|
target.Verify(x => x.InvalidateMeasure(root), Times.Once()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private IDisposable Start(ILayoutManager layoutManager) |
||||
|
{ |
||||
|
var result = AvaloniaLocator.EnterScope(); |
||||
|
AvaloniaLocator.CurrentMutable.Bind<ILayoutManager>().ToConstant(layoutManager); |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,41 +0,0 @@ |
|||||
// 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 Avalonia.Controls; |
|
||||
using Avalonia.Platform; |
|
||||
using Avalonia.Rendering; |
|
||||
|
|
||||
namespace Avalonia.Layout.UnitTests |
|
||||
{ |
|
||||
internal class TestLayoutRoot : Decorator, ILayoutRoot, IRenderRoot |
|
||||
{ |
|
||||
public TestLayoutRoot() |
|
||||
{ |
|
||||
ClientSize = new Size(500, 500); |
|
||||
} |
|
||||
|
|
||||
public Size ClientSize |
|
||||
{ |
|
||||
get; |
|
||||
set; |
|
||||
} |
|
||||
|
|
||||
public IRenderer Renderer => null; |
|
||||
|
|
||||
public IRenderTarget CreateRenderTarget() => null; |
|
||||
|
|
||||
public void Invalidate(Rect rect) |
|
||||
{ |
|
||||
} |
|
||||
|
|
||||
public Point PointToClient(Point point) => point; |
|
||||
|
|
||||
public Point PointToScreen(Point point) => point; |
|
||||
|
|
||||
public Size MaxClientSize => Size.Infinity; |
|
||||
public double LayoutScaling => 1; |
|
||||
|
|
||||
public ILayoutManager LayoutManager { get; set; } = new LayoutManager(); |
|
||||
|
|
||||
} |
|
||||
} |
|
||||
@ -0,0 +1,4 @@ |
|||||
|
<?xml version="1.0" encoding="utf-8"?> |
||||
|
<packages> |
||||
|
<package id="Cake" version="0.18.0" /> |
||||
|
</packages> |
||||
Loading…
Reference in new issue