123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- using System;
- using Avalonia.Layout;
- using Avalonia.UnitTests;
- using Xunit;
- namespace Avalonia.Controls.UnitTests
- {
- public class WrapPanelTests : ScopedTestBase
- {
- [Fact]
- public void Lays_Out_Horizontally_On_Separate_Lines()
- {
- var target = new WrapPanel()
- {
- Width = 100,
- Children =
- {
- new Border { Height = 50, Width = 100 },
- new Border { Height = 50, Width = 100 },
- }
- };
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(target.DesiredSize));
- Assert.Equal(new Size(100, 100), target.Bounds.Size);
- Assert.Equal(new Rect(0, 0, 100, 50), target.Children[0].Bounds);
- Assert.Equal(new Rect(0, 50, 100, 50), target.Children[1].Bounds);
- }
- [Fact]
- public void Lays_Out_Horizontally_On_A_Single_Line()
- {
- var target = new WrapPanel()
- {
- Width = 200,
- Children =
- {
- new Border { Height = 50, Width = 100 },
- new Border { Height = 50, Width = 100 },
- }
- };
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(target.DesiredSize));
- Assert.Equal(new Size(200, 50), target.Bounds.Size);
- Assert.Equal(new Rect(0, 0, 100, 50), target.Children[0].Bounds);
- Assert.Equal(new Rect(100, 0, 100, 50), target.Children[1].Bounds);
- }
- public static TheoryData<Orientation, WrapPanelItemsAlignment> GetItemsAlignmentValues()
- {
- var data = new TheoryData<Orientation, WrapPanelItemsAlignment>();
- foreach (var orientation in Enum.GetValues<Orientation>())
- {
- foreach (var alignment in Enum.GetValues<WrapPanelItemsAlignment>())
- {
- data.Add(orientation, alignment);
- }
- }
- return data;
- }
- [Theory, MemberData(nameof(GetItemsAlignmentValues))]
- public void Lays_Out_With_Items_Alignment(Orientation orientation, WrapPanelItemsAlignment itemsAlignment)
- {
- var target = new WrapPanel()
- {
- Width = 200,
- Height = 200,
- Orientation = orientation,
- ItemsAlignment = itemsAlignment,
- Children =
- {
- new Border { Height = 50, Width = 50 },
- new Border { Height = 50, Width = 50 },
- }
- };
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(target.DesiredSize));
- Assert.Equal(new Size(200, 200), target.Bounds.Size);
- var rowBounds = target.Children[0].Bounds.Union(target.Children[1].Bounds);
- Assert.Equal(orientation switch
- {
- Orientation.Horizontal => new(100, 50),
- Orientation.Vertical => new(50, 100),
- _ => throw new NotImplementedException()
- }, rowBounds.Size);
- Assert.Equal((orientation, itemsAlignment) switch
- {
- (_, WrapPanelItemsAlignment.Start) => new(0, 0),
- (Orientation.Horizontal, WrapPanelItemsAlignment.Center) => new(50, 0),
- (Orientation.Vertical, WrapPanelItemsAlignment.Center) => new(0, 50),
- (Orientation.Horizontal, WrapPanelItemsAlignment.End) => new(100, 0),
- (Orientation.Vertical, WrapPanelItemsAlignment.End) => new(0, 100),
- _ => throw new NotImplementedException(),
- }, rowBounds.Position);
- }
- [Fact]
- public void Lays_Out_Vertically_Children_On_A_Single_Line()
- {
- var target = new WrapPanel()
- {
- Orientation = Orientation.Vertical,
- Height = 120,
- Children =
- {
- new Border { Height = 50, Width = 100 },
- new Border { Height = 50, Width = 100 },
- }
- };
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(target.DesiredSize));
- Assert.Equal(new Size(100, 120), target.Bounds.Size);
- Assert.Equal(new Rect(0, 0, 100, 50), target.Children[0].Bounds);
- Assert.Equal(new Rect(0, 50, 100, 50), target.Children[1].Bounds);
- }
- [Fact]
- public void Lays_Out_Vertically_On_Separate_Lines()
- {
- var target = new WrapPanel()
- {
- Orientation = Orientation.Vertical,
- Height = 60,
- Children =
- {
- new Border { Height = 50, Width = 100 },
- new Border { Height = 50, Width = 100 },
- }
- };
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(target.DesiredSize));
- Assert.Equal(new Size(200, 60), target.Bounds.Size);
- Assert.Equal(new Rect(0, 0, 100, 50), target.Children[0].Bounds);
- Assert.Equal(new Rect(100, 0, 100, 50), target.Children[1].Bounds);
- }
- [Fact]
- public void Lays_Out_Horizontally_On_Separate_Lines_With_Spacing()
- {
- var target = new WrapPanel
- {
- Width = 100,
- ItemSpacing = 10,
- LineSpacing = 20,
- Children =
- {
- new Border { Height = 50, Width = 60 }, // line 0
- new Border { Height = 50, Width = 30 }, // line 0
- new Border { Height = 50, Width = 70 }, // line 1
- new Border { Height = 50, Width = 30 }, // line 2
- }
- };
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(target.DesiredSize));
- Assert.Equal(new Size(100, 190), target.Bounds.Size);
- Assert.Equal(new Rect(0, 0, 60, 50), target.Children[0].Bounds);
- Assert.Equal(new Rect(70, 0, 30, 50), target.Children[1].Bounds);
- Assert.Equal(new Rect(0, 70, 70, 50), target.Children[2].Bounds);
- Assert.Equal(new Rect(0, 140, 30, 50), target.Children[3].Bounds);
- }
- [Fact]
- public void Lays_Out_Horizontally_On_Separate_Lines_With_Spacing_Invisible()
- {
- var target = new WrapPanel
- {
- ItemSpacing = 10,
- Children =
- {
- new Border { Height = 50, Width = 60 }, // line 0
- new Border { Height = 50, Width = 30 , IsVisible = false }, // line 0
- new Border { Height = 50, Width = 50 }, // line 0
- }
- };
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(target.DesiredSize));
- Assert.Equal(new Size(120, 50), target.Bounds.Size);
- Assert.Equal(new Rect(0, 0, 60, 50), target.Children[0].Bounds);
- Assert.Equal(new Rect(70, 0, 50, 50), target.Children[2].Bounds);
- }
- [Fact]
- public void Lays_Out_Horizontally_On_Separate_Lines_With_Spacing_Vertical()
- {
- var target = new WrapPanel
- {
- Height = 100,
- Orientation = Orientation.Vertical,
- ItemSpacing = 10,
- LineSpacing = 20,
- Children =
- {
- new Border { Width = 50, Height = 60 }, // line 0
- new Border { Width = 50, Height = 30 }, // line 0
- new Border { Width = 50, Height = 70 }, // line 1
- new Border { Width = 50, Height = 30 }, // line 2
- }
- };
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(target.DesiredSize));
- Assert.Equal(new Size(190, 100), target.Bounds.Size);
- Assert.Equal(new Rect(0, 0, 50, 60), target.Children[0].Bounds);
- Assert.Equal(new Rect(0, 70, 50, 30), target.Children[1].Bounds);
- Assert.Equal(new Rect(70, 0, 50, 70), target.Children[2].Bounds);
- Assert.Equal(new Rect(140, 0, 50, 30), target.Children[3].Bounds);
- }
- [Fact]
- public void Applies_ItemWidth_And_ItemHeight_Properties()
- {
- var target = new WrapPanel()
- {
- Orientation = Orientation.Horizontal,
- Width = 50,
- ItemWidth = 20,
- ItemHeight = 15,
- Children =
- {
- new Border(),
- new Border(),
- }
- };
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(target.DesiredSize));
- Assert.Equal(new Size(50, 15), target.Bounds.Size);
- Assert.Equal(new Rect(0, 0, 20, 15), target.Children[0].Bounds);
- Assert.Equal(new Rect(20, 0, 20, 15), target.Children[1].Bounds);
- }
- [Fact]
- public void Zero_Size_Visible_Child()
- {
- var target = new WrapPanel()
- {
- Orientation = Orientation.Horizontal,
- Width = 50,
- ItemSpacing = 10,
- LineSpacing = 10,
- Children =
- {
- new Border(), // line 0
- new Border // line 1
- {
- Width = 50,
- Height = 50
- },
- }
- };
- target.Measure(Size.Infinity);
- target.Arrange(new Rect(target.DesiredSize));
- Assert.Equal(new Size(50, 60), target.Bounds.Size);
- Assert.Equal(new Rect(0, 0, 0, 0), target.Children[0].Bounds);
- Assert.Equal(new Rect(0, 10, 50, 50), target.Children[1].Bounds);
- }
- [Fact]
- void ItemWidth_Trigger_InvalidateMeasure()
- {
- var target = new WrapPanel();
- target.Measure(new Size(10, 10));
- Assert.True(target.IsMeasureValid);
- target.ItemWidth = 1;
- Assert.False(target.IsMeasureValid);
- }
- [Fact]
- void ItemHeight_Trigger_InvalidateMeasure()
- {
- var target = new WrapPanel();
- target.Measure(new Size(10, 10));
- Assert.True(target.IsMeasureValid);
- target.ItemHeight = 1;
- Assert.False(target.IsMeasureValid);
- }
- }
- }
|