1
0

DecoratorTests.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System.Collections.Specialized;
  2. using System.Linq;
  3. using Avalonia.LogicalTree;
  4. using Avalonia.UnitTests;
  5. using Xunit;
  6. namespace Avalonia.Controls.UnitTests
  7. {
  8. public class DecoratorTests : ScopedTestBase
  9. {
  10. [Fact]
  11. public void Setting_Content_Should_Set_Child_Controls_Parent()
  12. {
  13. var decorator = new Decorator();
  14. var child = new Control();
  15. decorator.Child = child;
  16. Assert.Equal(child.Parent, decorator);
  17. Assert.Equal(((ILogical)child).LogicalParent, decorator);
  18. }
  19. [Fact]
  20. public void Clearing_Content_Should_Clear_Child_Controls_Parent()
  21. {
  22. var decorator = new Decorator();
  23. var child = new Control();
  24. decorator.Child = child;
  25. decorator.Child = null;
  26. Assert.Null(child.Parent);
  27. Assert.Null(((ILogical)child).LogicalParent);
  28. }
  29. [Fact]
  30. public void Content_Control_Should_Appear_In_LogicalChildren()
  31. {
  32. var decorator = new Decorator();
  33. var child = new Control();
  34. decorator.Child = child;
  35. Assert.Equal(new[] { child }, ((ILogical)decorator).LogicalChildren.ToList());
  36. }
  37. [Fact]
  38. public void Clearing_Content_Should_Remove_From_LogicalChildren()
  39. {
  40. var decorator = new Decorator();
  41. var child = new Control();
  42. decorator.Child = child;
  43. decorator.Child = null;
  44. Assert.Equal(new ILogical[0], ((ILogical)decorator).LogicalChildren.ToList());
  45. }
  46. [Fact]
  47. public void Setting_Content_Should_Fire_LogicalChildren_CollectionChanged()
  48. {
  49. var decorator = new Decorator();
  50. var child = new Control();
  51. var called = false;
  52. ((ILogical)decorator).LogicalChildren.CollectionChanged += (s, e) =>
  53. called = e.Action == NotifyCollectionChangedAction.Add;
  54. decorator.Child = child;
  55. Assert.True(called);
  56. }
  57. [Fact]
  58. public void Clearing_Content_Should_Fire_LogicalChildren_CollectionChanged()
  59. {
  60. var decorator = new Decorator();
  61. var child = new Control();
  62. var called = false;
  63. decorator.Child = child;
  64. ((ILogical)decorator).LogicalChildren.CollectionChanged += (s, e) =>
  65. called = e.Action == NotifyCollectionChangedAction.Remove;
  66. decorator.Child = null;
  67. Assert.True(called);
  68. }
  69. [Fact]
  70. public void Changing_Content_Should_Fire_LogicalChildren_CollectionChanged()
  71. {
  72. var decorator = new Decorator();
  73. var child1 = new Control();
  74. var child2 = new Control();
  75. var called = false;
  76. decorator.Child = child1;
  77. ((ILogical)decorator).LogicalChildren.CollectionChanged += (s, e) => called = true;
  78. decorator.Child = child2;
  79. Assert.True(called);
  80. }
  81. [Fact]
  82. public void Measure_Should_Return_Padding_When_No_Child_Present()
  83. {
  84. var target = new Decorator
  85. {
  86. Padding = new Thickness(8),
  87. };
  88. target.Measure(new Size(100, 100));
  89. Assert.Equal(new Size(16, 16), target.DesiredSize);
  90. }
  91. public class UseLayoutRounding : ScopedTestBase
  92. {
  93. [Fact]
  94. public void Measure_Rounds_Padding()
  95. {
  96. var target = new Decorator
  97. {
  98. Padding = new Thickness(1),
  99. Child = new Canvas
  100. {
  101. Width = 101,
  102. Height = 101,
  103. }
  104. };
  105. var root = CreatedRoot(1.5, target);
  106. root.LayoutManager.ExecuteInitialLayoutPass();
  107. // - 1 pixel padding is rounded up to 1.3333; for both sides it is 2.6666
  108. // - Size of 101 gets rounded up to 101.3333
  109. // - Desired size = 101.3333 + 2.6666 = 104
  110. Assert.Equal(new Size(104, 104), target.DesiredSize);
  111. }
  112. private static TestRoot CreatedRoot(
  113. double scaling,
  114. Control child,
  115. Size? constraint = null)
  116. {
  117. return new TestRoot
  118. {
  119. LayoutScaling = scaling,
  120. UseLayoutRounding = true,
  121. Child = child,
  122. ClientSize = constraint ?? new Size(1000, 1000),
  123. };
  124. }
  125. }
  126. }
  127. }