ArrangeTests.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright (c) The Perspex Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using Perspex.Controls;
  4. using Xunit;
  5. namespace Perspex.Layout.UnitTests
  6. {
  7. public class ArrangeTests
  8. {
  9. [Fact]
  10. public void Margin_Should_Be_Subtracted_From_Arrange_FinalSize()
  11. {
  12. var target = new TestControl
  13. {
  14. Width = 100,
  15. Height = 100,
  16. Margin = new Thickness(8),
  17. };
  18. target.Measure(Size.Infinity);
  19. target.Arrange(new Rect(target.DesiredSize));
  20. Assert.Equal(new Size(100, 100), target.ArrangeFinalSize);
  21. }
  22. [Fact]
  23. public void ArrangeOverride_Receives_Desired_Size_When_Centered()
  24. {
  25. var target = new TestControl
  26. {
  27. MeasureResult = new Size(100, 100),
  28. HorizontalAlignment = HorizontalAlignment.Center,
  29. VerticalAlignment = VerticalAlignment.Center,
  30. Margin = new Thickness(8),
  31. };
  32. target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  33. target.Arrange(new Rect(0, 0, 200, 200));
  34. Assert.Equal(new Size(100, 100), target.ArrangeFinalSize);
  35. }
  36. [Fact]
  37. public void ArrangeOverride_Receives_Available_Size_Minus_Margin_When_Stretched()
  38. {
  39. var target = new TestControl
  40. {
  41. MeasureResult = new Size(100, 100),
  42. HorizontalAlignment = HorizontalAlignment.Stretch,
  43. VerticalAlignment = VerticalAlignment.Stretch,
  44. Margin = new Thickness(8),
  45. };
  46. target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  47. target.Arrange(new Rect(0, 0, 200, 200));
  48. Assert.Equal(new Size(184, 184), target.ArrangeFinalSize);
  49. }
  50. [Fact]
  51. public void ArrangeOverride_Receives_Requested_Size_When_Arranged_To_DesiredSize()
  52. {
  53. var target = new TestControl
  54. {
  55. MeasureResult = new Size(100, 100),
  56. HorizontalAlignment = HorizontalAlignment.Center,
  57. VerticalAlignment = VerticalAlignment.Center,
  58. Margin = new Thickness(8),
  59. };
  60. target.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
  61. target.Arrange(new Rect(target.DesiredSize));
  62. Assert.Equal(new Size(100, 100), target.ArrangeFinalSize);
  63. }
  64. [Fact]
  65. public void Arrange_With_IsMeasureValid_False_Calls_Measure()
  66. {
  67. var target = new TestControl();
  68. Assert.False(target.IsMeasureValid);
  69. target.Arrange(new Rect(0, 0, 120, 120));
  70. Assert.True(target.IsMeasureValid);
  71. Assert.Equal(new Size(120, 120), target.MeasureConstraint);
  72. }
  73. [Fact]
  74. public void Arrange_With_IsMeasureValid_False_Calls_Measure_With_Previous_Size_If_Available()
  75. {
  76. var target = new TestControl();
  77. Assert.False(target.IsMeasureValid);
  78. target.Arrange(new Rect(0, 0, 120, 120));
  79. target.InvalidateMeasure();
  80. target.Arrange(new Rect(0, 0, 100, 100));
  81. Assert.True(target.IsMeasureValid);
  82. Assert.Equal(new Size(120, 120), target.MeasureConstraint);
  83. }
  84. private class TestControl : Decorator
  85. {
  86. public Size MeasureConstraint { get; private set; }
  87. public Size MeasureResult { get; set; }
  88. public Size ArrangeFinalSize { get; private set; }
  89. protected override Size MeasureOverride(Size constraint)
  90. {
  91. MeasureConstraint = constraint;
  92. return MeasureResult;
  93. }
  94. protected override Size ArrangeOverride(Size finalSize)
  95. {
  96. ArrangeFinalSize = finalSize;
  97. return base.ArrangeOverride(finalSize);
  98. }
  99. }
  100. }
  101. }