| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- using System;
- using Avalonia.Controls;
- using Moq;
- using Xunit;
- namespace Avalonia.Layout.UnitTests
- {
- public class LayoutableTests
- {
- [Theory]
- [InlineData(0, 0, 0, 0, 100, 100)]
- [InlineData(10, 0, 0, 0, 90, 100)]
- [InlineData(10, 0, 5, 0, 85, 100)]
- [InlineData(0, 10, 0, 0, 100, 90)]
- [InlineData(0, 10, 0, 5, 100, 85)]
- [InlineData(4, 4, 6, 7, 90, 89)]
- public void Margin_Is_Applied_To_MeasureOverride_Size(
- double l,
- double t,
- double r,
- double b,
- double expectedWidth,
- double expectedHeight)
- {
- var target = new TestLayoutable
- {
- Margin = new Thickness(l, t, r, b),
- };
- target.Measure(new Size(100, 100));
- Assert.Equal(new Size(expectedWidth, expectedHeight), target.MeasureSize);
- }
- [Theory]
- [InlineData(HorizontalAlignment.Stretch, 100)]
- [InlineData(HorizontalAlignment.Left, 10)]
- [InlineData(HorizontalAlignment.Center, 10)]
- [InlineData(HorizontalAlignment.Right, 10)]
- public void HorizontalAlignment_Is_Applied_To_ArrangeOverride_Size(
- HorizontalAlignment h,
- double expectedWidth)
- {
- var target = new TestLayoutable
- {
- HorizontalAlignment = h,
- };
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Size(expectedWidth, 100), target.ArrangeSize);
- }
- [Theory]
- [InlineData(VerticalAlignment.Stretch, 100)]
- [InlineData(VerticalAlignment.Top, 10)]
- [InlineData(VerticalAlignment.Center, 10)]
- [InlineData(VerticalAlignment.Bottom, 10)]
- public void VerticalAlignment_Is_Applied_To_ArrangeOverride_Size(
- VerticalAlignment v,
- double expectedHeight)
- {
- var target = new TestLayoutable
- {
- VerticalAlignment = v,
- };
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Size(100, expectedHeight), target.ArrangeSize);
- }
- [Theory]
- [InlineData(0, 0, 0, 0, 100, 100)]
- [InlineData(10, 0, 0, 0, 90, 100)]
- [InlineData(10, 0, 5, 0, 85, 100)]
- [InlineData(0, 10, 0, 0, 100, 90)]
- [InlineData(0, 10, 0, 5, 100, 85)]
- [InlineData(4, 4, 6, 7, 90, 89)]
- public void Margin_Is_Applied_To_ArrangeOverride_Size(
- double l,
- double t,
- double r,
- double b,
- double expectedWidth,
- double expectedHeight)
- {
- var target = new TestLayoutable
- {
- Margin = new Thickness(l, t, r, b),
- };
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Size(expectedWidth, expectedHeight), target.ArrangeSize);
- }
- [Fact]
- public void Only_Calls_LayoutManager_InvalidateMeasure_Once()
- {
- var target = new Mock<ILayoutManager>();
- var control = new Decorator();
- var root = new LayoutTestRoot
- {
- Child = control,
- LayoutManager = target.Object,
- };
- 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>();
- var control = new Decorator();
- var root = new LayoutTestRoot
- {
- Child = control,
- LayoutManager = target.Object,
- };
- 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>();
- var control = new Decorator();
- var root = new LayoutTestRoot
- {
- Child = control,
- LayoutManager = target.Object,
- };
- 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 class TestLayoutable : Layoutable
- {
- public Size ArrangeSize { get; private set; }
- public Size MeasureResult { get; set; } = new Size(10, 10);
- public Size MeasureSize { get; private set; }
- protected override Size MeasureOverride(Size availableSize)
- {
- MeasureSize = availableSize;
- return MeasureResult;
- }
- protected override Size ArrangeOverride(Size finalSize)
- {
- ArrangeSize = finalSize;
- return base.ArrangeOverride(finalSize);
- }
- }
- }
- }
|