DockPanelTests.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. using Avalonia.UnitTests;
  2. using Xunit;
  3. namespace Avalonia.Controls.UnitTests
  4. {
  5. public class DockPanelTests : ScopedTestBase
  6. {
  7. [Fact]
  8. public void DockPanel_Without_Child()
  9. {
  10. var target = new DockPanel
  11. {
  12. Width = 10,
  13. Height = 10
  14. };
  15. target.Measure(Size.Infinity);
  16. target.Arrange(new Rect(target.DesiredSize));
  17. Assert.Equal(new Rect(0, 0, 10, 10), target.Bounds);
  18. }
  19. [Fact]
  20. public void Should_Dock_Controls_Horizontal_First()
  21. {
  22. var target = new DockPanel
  23. {
  24. Children =
  25. {
  26. new Border { Width = 500, Height = 50, [DockPanel.DockProperty] = Dock.Top },
  27. new Border { Width = 500, Height = 50, [DockPanel.DockProperty] = Dock.Bottom },
  28. new Border { Width = 50, Height = 400, [DockPanel.DockProperty] = Dock.Left },
  29. new Border { Width = 50, Height = 400, [DockPanel.DockProperty] = Dock.Right },
  30. new Border { },
  31. }
  32. };
  33. target.Measure(Size.Infinity);
  34. target.Arrange(new Rect(target.DesiredSize));
  35. Assert.Equal(new Rect(0, 0, 500, 500), target.Bounds);
  36. Assert.Equal(new Rect(0, 0, 500, 50), target.Children[0].Bounds);
  37. Assert.Equal(new Rect(0, 450, 500, 50), target.Children[1].Bounds);
  38. Assert.Equal(new Rect(0, 50, 50, 400), target.Children[2].Bounds);
  39. Assert.Equal(new Rect(450, 50, 50, 400), target.Children[3].Bounds);
  40. Assert.Equal(new Rect(50, 50, 400, 400), target.Children[4].Bounds);
  41. }
  42. [Fact]
  43. public void Should_Dock_Controls_Vertical_First()
  44. {
  45. var target = new DockPanel
  46. {
  47. Children =
  48. {
  49. new Border { Width = 50, Height = 400, [DockPanel.DockProperty] = Dock.Left },
  50. new Border { Width = 50, Height = 400, [DockPanel.DockProperty] = Dock.Right },
  51. new Border { Width = 500, Height = 50, [DockPanel.DockProperty] = Dock.Top },
  52. new Border { Width = 500, Height = 50, [DockPanel.DockProperty] = Dock.Bottom },
  53. new Border { },
  54. }
  55. };
  56. target.Measure(Size.Infinity);
  57. target.Arrange(new Rect(target.DesiredSize));
  58. Assert.Equal(new Rect(0, 0, 600, 400), target.Bounds);
  59. Assert.Equal(new Rect(0, 0, 50, 400), target.Children[0].Bounds);
  60. Assert.Equal(new Rect(550, 0, 50, 400), target.Children[1].Bounds);
  61. Assert.Equal(new Rect(50, 0, 500, 50), target.Children[2].Bounds);
  62. Assert.Equal(new Rect(50, 350, 500, 50), target.Children[3].Bounds);
  63. Assert.Equal(new Rect(50, 50, 500, 300), target.Children[4].Bounds);
  64. }
  65. [Fact]
  66. public void Should_Dock_Controls_With_Spacing()
  67. {
  68. var target = new DockPanel
  69. {
  70. HorizontalSpacing = 10,
  71. VerticalSpacing = 10,
  72. Children =
  73. {
  74. new Border { Width = 500, Height = 50, [DockPanel.DockProperty] = Dock.Top },
  75. new Border { Width = 500, Height = 50, [DockPanel.DockProperty] = Dock.Bottom },
  76. new Border { Width = 50, Height = 400, [DockPanel.DockProperty] = Dock.Left },
  77. new Border { Width = 50, Height = 400, [DockPanel.DockProperty] = Dock.Right },
  78. new Border { },
  79. }
  80. };
  81. target.Measure(Size.Infinity);
  82. target.Arrange(new Rect(target.DesiredSize));
  83. Assert.Equal(new Rect(0, 0, 500, 520), target.Bounds);
  84. Assert.Equal(new Rect(0, 0, 500, 50), target.Children[0].Bounds);
  85. Assert.Equal(new Rect(0, 470, 500, 50), target.Children[1].Bounds);
  86. Assert.Equal(new Rect(0, 60, 50, 400), target.Children[2].Bounds);
  87. Assert.Equal(new Rect(450, 60, 50, 400), target.Children[3].Bounds);
  88. Assert.Equal(new Rect(60, 60, 380, 400), target.Children[4].Bounds);
  89. }
  90. [Fact]
  91. public void Changing_Child_Dock_Invalidates_Measure()
  92. {
  93. Border child;
  94. var target = new DockPanel
  95. {
  96. Children =
  97. {
  98. (child = new Border
  99. {
  100. [DockPanel.DockProperty] = Dock.Left,
  101. }),
  102. }
  103. };
  104. target.Measure(Size.Infinity);
  105. target.Arrange(new Rect(target.DesiredSize));
  106. Assert.True(target.IsMeasureValid);
  107. DockPanel.SetDock(child, Dock.Right);
  108. Assert.False(target.IsMeasureValid);
  109. }
  110. }
  111. }