123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- using Avalonia.Controls.Presenters;
- using Avalonia.Layout;
- using Avalonia.UnitTests;
- using Xunit;
- namespace Avalonia.Controls.UnitTests.Presenters
- {
- public class ContentPresenterTests_Layout : ScopedTestBase
- {
- [Theory]
- [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Stretch, 0, 0, 100, 100)]
- [InlineData(HorizontalAlignment.Left, VerticalAlignment.Stretch, 0, 0, 16, 100)]
- [InlineData(HorizontalAlignment.Right, VerticalAlignment.Stretch, 84, 0, 16, 100)]
- [InlineData(HorizontalAlignment.Center, VerticalAlignment.Stretch, 42, 0, 16, 100)]
- [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Top, 0, 0, 100, 16)]
- [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Bottom, 0, 84, 100, 16)]
- [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Center, 0, 42, 100, 16)]
- public void Content_Alignment_Is_Applied_To_Child_Bounds(
- HorizontalAlignment h,
- VerticalAlignment v,
- double expectedX,
- double expectedY,
- double expectedWidth,
- double expectedHeight)
- {
- Border content;
- var target = new ContentPresenter
- {
- HorizontalContentAlignment = h,
- VerticalContentAlignment = v,
- Content = content = new Border
- {
- MinWidth = 16,
- MinHeight = 16,
- },
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Rect(expectedX, expectedY, expectedWidth, expectedHeight), content.Bounds);
- }
- [Theory]
- [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Stretch, 10, 10, 80, 80)]
- [InlineData(HorizontalAlignment.Left, VerticalAlignment.Stretch, 10, 10, 16, 80)]
- [InlineData(HorizontalAlignment.Right, VerticalAlignment.Stretch, 74, 10, 16, 80)]
- [InlineData(HorizontalAlignment.Center, VerticalAlignment.Stretch, 42, 10, 16, 80)]
- [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Top, 10, 10, 80, 16)]
- [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Bottom, 10, 74, 80, 16)]
- [InlineData(HorizontalAlignment.Stretch, VerticalAlignment.Center, 10, 42, 80, 16)]
- public void Content_Alignment_And_Padding_Are_Applied_To_Child_Bounds(
- HorizontalAlignment h,
- VerticalAlignment v,
- double expectedX,
- double expectedY,
- double expectedWidth,
- double expectedHeight)
- {
- Border content;
- var target = new ContentPresenter
- {
- HorizontalContentAlignment = h,
- VerticalContentAlignment = v,
- Padding = new Thickness(10),
- Content = content = new Border
- {
- MinWidth = 16,
- MinHeight = 16,
- },
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Rect(expectedX, expectedY, expectedWidth, expectedHeight), content.Bounds);
- }
- [Fact]
- public void Should_Correctly_Align_Child_With_Fixed_Size()
- {
- Border content;
- var target = new ContentPresenter
- {
- HorizontalContentAlignment = HorizontalAlignment.Stretch,
- VerticalContentAlignment = VerticalAlignment.Stretch,
- Content = content = new Border
- {
- HorizontalAlignment = HorizontalAlignment.Left,
- VerticalAlignment = VerticalAlignment.Bottom,
- Width = 16,
- Height = 16,
- },
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- // Check correct result for Issue #1447.
- Assert.Equal(new Rect(0, 84, 16, 16), content.Bounds);
- }
- [Fact]
- public void Content_Can_Be_Stretched()
- {
- Border content;
- var target = new ContentPresenter
- {
- Content = content = new Border
- {
- MinWidth = 16,
- MinHeight = 16,
- },
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Rect(0, 0, 100, 100), content.Bounds);
- }
- [Fact]
- public void Content_Can_Be_Right_Aligned()
- {
- Border content;
- var target = new ContentPresenter
- {
- Content = content = new Border
- {
- MinWidth = 16,
- MinHeight = 16,
- HorizontalAlignment = HorizontalAlignment.Right
- },
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Rect(84, 0, 16, 100), content.Bounds);
- }
- [Fact]
- public void Content_Can_Be_Bottom_Aligned()
- {
- Border content;
- var target = new ContentPresenter
- {
- Content = content = new Border
- {
- MinWidth = 16,
- MinHeight = 16,
- VerticalAlignment = VerticalAlignment.Bottom,
- },
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Rect(0, 84, 100, 16), content.Bounds);
- }
- [Fact]
- public void Content_Can_Be_TopLeft_Aligned()
- {
- Border content;
- var target = new ContentPresenter
- {
- Content = content = new Border
- {
- MinWidth = 16,
- MinHeight = 16,
- HorizontalAlignment = HorizontalAlignment.Right,
- VerticalAlignment = VerticalAlignment.Top,
- },
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Rect(84, 0, 16, 16), content.Bounds);
- }
- [Fact]
- public void Content_Can_Be_TopRight_Aligned()
- {
- Border content;
- var target = new ContentPresenter
- {
- Content = content = new Border
- {
- MinWidth = 16,
- MinHeight = 16,
- HorizontalAlignment = HorizontalAlignment.Right,
- VerticalAlignment = VerticalAlignment.Top,
- },
- };
- target.UpdateChild();
- target.Measure(new Size(100, 100));
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Rect(84, 0, 16, 16), content.Bounds);
- }
- [Fact]
- public void Child_Arrange_With_Zero_Height_When_Padding_Height_Greater_Than_Child_Height()
- {
- Border content;
- var target = new ContentPresenter
- {
- Padding = new Thickness(32),
- MaxHeight = 32,
- MaxWidth = 32,
- HorizontalContentAlignment = HorizontalAlignment.Center,
- VerticalContentAlignment = VerticalAlignment.Center,
- Content = content = new Border
- {
- Height = 0,
- Width = 0,
- },
- };
- target.UpdateChild();
- target.Arrange(new Rect(0, 0, 100, 100));
- Assert.Equal(new Rect(32, 32, 0, 0), content.Bounds);
- }
- public class UseLayoutRounding : ScopedTestBase
- {
- [Fact]
- public void Measure_Rounds_Padding()
- {
- var target = new ContentPresenter
- {
- Padding = new Thickness(1),
- Content = new Canvas
- {
- Width = 101,
- Height = 101,
- }
- };
- var root = CreatedRoot(1.5, target);
- root.LayoutManager.ExecuteInitialLayoutPass();
- // - 1 pixel padding is rounded up to 1.3333; for both sides it is 2.6666
- // - Size of 101 gets rounded up to 101.3333
- // - Desired size = 101.3333 + 2.6666 = 104
- Assert.Equal(new Size(104, 104), target.DesiredSize);
- }
- [Fact]
- public void Measure_Rounds_BorderThickness()
- {
- var target = new ContentPresenter
- {
- BorderThickness = new Thickness(1),
- Content = new Canvas
- {
- Width = 101,
- Height = 101,
- }
- };
- var root = CreatedRoot(1.5, target);
- root.LayoutManager.ExecuteInitialLayoutPass();
- // - 1 pixel border thickness is rounded up to 1.3333; for both sides it is 2.6666
- // - Size of 101 gets rounded up to 101.3333
- // - Desired size = 101.3333 + 2.6666 = 104
- Assert.Equal(new Size(104, 104), target.DesiredSize);
- }
- private static TestRoot CreatedRoot(
- double scaling,
- Control child,
- Size? constraint = null)
- {
- return new TestRoot
- {
- LayoutScaling = scaling,
- UseLayoutRounding = true,
- Child = child,
- ClientSize = constraint ?? new Size(1000, 1000),
- };
- }
- }
- }
- }
|